Connecting Tech Pros Worldwide Forums | Help | Site Map

How to loop through element IDs that begin with a prefix (i.e. "item_" + variable)?

Sugapablo
Guest
 
Posts: n/a
#1: Dec 7 '05
Real quick question:

I have a bunch of elements on a page, each has a prefix of "item_". So
for any page I could haev a series of element IDs that look like:

id="item_49"
id="item_45"
id="item_59"
id="item_56"

How do I make a quick loop through just the element IDs on the page that
begin with "item_"?

--
Sugapablo
http://www.sugapablo.net



McKirahan
Guest
 
Posts: n/a
#2: Dec 7 '05

re: How to loop through element IDs that begin with a prefix (i.e. "item_" + variable)?


"Sugapablo" <russ@REMOVEsugapablo.com> wrote in message
news:pan.2005.12.07.19.30.06.811767@REMOVEsugapabl o.com...[color=blue]
> Real quick question:
>
> I have a bunch of elements on a page, each has a prefix of "item_". So
> for any page I could haev a series of element IDs that look like:
>
> id="item_49"
> id="item_45"
> id="item_59"
> id="item_56"
>
> How do I make a quick loop through just the element IDs on the page that
> begin with "item_"?
>
> --
> Sugapablo
> http://www.sugapablo.net
>
>[/color]

WIll this help?


for (var i=49; i<57; i++) {
alert(document.getElementById("item_"+i).value;
}


web.dev
Guest
 
Posts: n/a
#3: Dec 7 '05

re: How to loop through element IDs that begin with a prefix (i.e. "item_" + variable)?



Sugapablo wrote:[color=blue]
> Real quick question:
>
> I have a bunch of elements on a page, each has a prefix of "item_". So
> for any page I could haev a series of element IDs that look like:
>
> id="item_49"
> id="item_45"
> id="item_59"
> id="item_56"
>
> How do I make a quick loop through just the element IDs on the page that
> begin with "item_"?
>
> --
> Sugapablo
> http://www.sugapablo.net[/color]

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.

Sugapablo
Guest
 
Posts: n/a
#4: Dec 7 '05

re: How to loop through element IDs that begin with a prefix (i.e. "item_" + variable)?


On Wed, 07 Dec 2005 12:16:32 -0800, web.dev wrote:

[color=blue]
> 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.[/color]

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.

--
Sugapablo
http://www.sugapablo.net


Ivo
Guest
 
Posts: n/a
#5: Dec 7 '05

re: How to loop through element IDs that begin with a prefix (i.e. "item_" + variable)?


"Sugapablo" wrote[color=blue]
> web.dev wrote:[color=green]
> > 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.[/color]
>
> 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.[/color]

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/


Jasen Betts
Guest
 
Posts: n/a
#6: Dec 8 '05

re: How to loop through element IDs that begin with a prefix (i.e. "item_" + variable)?


On 2005-12-07, Sugapablo <russ@REMOVEsugapablo.com> wrote:[color=blue]
> On Wed, 07 Dec 2005 12:16:32 -0800, web.dev wrote:[/color]
[color=blue][color=green]
>> Obviously, you can change the starting point and the max number.[/color]
>
> 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.[/color]

then do it some other way, possibly give them all the samle class and
use a stylesheet...

or only address their container and then resolve the events you receive
down to the item under the pointer etc...

otoh: 1000 itmes need not take a long time to process.


--

Bye.
Jasen
Sugapablo
Guest
 
Posts: n/a
#7: Dec 8 '05

re: How to loop through element IDs that begin with a prefix (i.e. "item_" + variable)?


On Wed, 07 Dec 2005 12:43:34 -0800, Lee wrote:
[color=blue]
> So why did you ask how to loop through them?
> What are you really looking for?[/color]

I'm trying to narrow down what I'm looping through.

I'd like some sort of functionality close to looping through only id's on
the page that start with "item_".

I'm trying to determine if that's possible.

Something like:

foreach(getElementsByID("item_"*) {

}

--
Sugapablo
http://www.sugapablo.net

Closed Thread