"Sugapablo" wrote
web.dev wrote: You can do something like the following:
for(var i = 0; i < max_number; ++i)
{
var elem = document.getElementById("item_" + i);
...[code statements]...
}
Obviously, you can change the starting point and the max number.
Yeah, I knew that much, but the number can be any integer. I'm assuming
up to the thousands. That's way too much to loop through.
That is some important information you didn't tell us at first. It sounds
like you need to get rid of id's in general and walk through a collection of
elements by tagname or some other comon denominator.
If you stick to id's, look at this:
for( var a = document.getElementsByTagName( 'whatever' ), i=0, n = a.length;
i<n; i++ ) {
if( a[i].id && a[i].id.indexOf( 'item_' )===0 ) {
// got one, i can do my thing now...
}
}
BTW, do loops and while loops are generally faster than for loops. And
counting down is faster than up, and comparing to zero is faster than
comparing other numbers. With that in mind, compare this:
var a = document.getElementsByTagName( 'whatever' ), i = a.length;
while( i-- ) {
if( a[i].id && a[i].id.indexOf( 'item_' )===0 ) {
// got one, let me do my thing now...
}
}
hth
ivo
http://4umi.com/web/javascript/