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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×