Jump to content
Zerivo

how make many id in one getElementById

Recommended Posts

Hi, i make one but it will be many line same, like this with 20.

document.getElementById("Test1").value = Dotoff;
document.getElementById("Test2").value = Dotoff;
document.getElementById("Test3").value = Dotoff;

i wonder can it make all id in one? i tried it:
 

document.getElementById("Test1","Test2","Test3").value = Dotoff;


but it didn't work well, only Test1 change.
some know how make all 20 in one? it can save me lots space/line ;)

Share this post


Link to post
Share on other sites

can you show me example? btw Dotoff is text inside, i use this code
var Dotoff = String.fromCharCode(9678); 

which look like: ◎

Share this post


Link to post
Share on other sites

'dsonesuk's idea more dynamic with code changes, but you could also do it old style in a pinch.

<script>
for (var i=1; i<=20; i++) { document.getElementById("Test"+i).value = Dotoff; }
</script>
	

 

Share this post


Link to post
Share on other sites

Then too, you could use jQuery, it will do the counting for you.

document.getElementById("Test1").value = Dotoff;
document.getElementById("Test2").value = Dotoff;
document.getElementById("Test3").value = Dotoff;

<element1 class='test'></element1>
<element2 class='test'></element2>
<element3 class='test'></element3>

$('.test').val('Dotoff');

Roddy

Share this post


Link to post
Share on other sites

same thing in javascript:

var test = document.getElementsByClassName("test");

for (var i=0;i<test.length;i++){

    test[i].value = Dotoff;

}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...