By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,043 Members | 1,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,043 IT Pros & Developers. It's quick & easy.

call an ID range

P: n/a
Amy
let say i have an id range Ie. test1, test2, test3 ...

How do i call all of them at a time ? I cant use getElementbyId for
sure ...

Anyone has any clues?

Jul 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Amy said the following on 7/20/2006 2:56 AM:
let say i have an id range Ie. test1, test2, test3 ...

How do i call all of them at a time ? I cant use getElementbyId for
sure ...
Actually, you can. You just set a loop:

for (var i=1;i<4;i++){
thisID = document.getElementById('test' + i)
//do something with thisID
}

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 20 '06 #2

P: n/a
Amy wrote:
let say i have an id range Ie. test1, test2, test3 ...

How do i call all of them at a time ? I cant use getElementbyId for
sure ...
If you know the number of elements, you can always try a for-loop:

for (i = 0; i < MAX; i++) {
e = document.getElementById('test' + i);
if (e == null) continue;
// do something with e
}

Another option is to give every element the same _name_, you can query
those:

// returns array of objects named 'test'.
t = document.getElementsByName('test');

If you know the type of the elements you can query
getElementsByTagName() in the same way as you query the getelements. You
can check with startsWith whether its the right object:

t = document.getElementsByTagName('input');

for (i = 0; i < t.length; i++) {
if (t.id.startsWith('test')) {
// do somethnig
}
}

If all else fails you'll have to walk the DOM tree recusivly, or if you
only target IE, you can use the all[] array to walk through all elements.

Good luck,
Vincent
Jul 20 '06 #3

P: n/a
Amy
cool ..that works
Randy Webb wrote:
Amy said the following on 7/20/2006 2:56 AM:
let say i have an id range Ie. test1, test2, test3 ...

How do i call all of them at a time ? I cant use getElementbyId for
sure ...

Actually, you can. You just set a loop:

for (var i=1;i<4;i++){
thisID = document.getElementById('test' + i)
//do something with thisID
}

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.