469,352 Members | 1,806 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

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

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
Dec 7 '05 #1
6 20679
"Sugapablo" <ru**@REMOVEsugapablo.com> wrote in message
news:pa****************************@REMOVEsugapabl o.com...
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


WIll this help?
for (var i=49; i<57; i++) {
alert(document.getElementById("item_"+i).value;
}
Dec 7 '05 #2

Sugapablo wrote:
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


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.

Dec 7 '05 #3
On Wed, 07 Dec 2005 12:16:32 -0800, 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.

--
Sugapablo
http://www.sugapablo.net
Dec 7 '05 #4
Ivo
"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/
Dec 7 '05 #5
On 2005-12-07, Sugapablo <ru**@REMOVEsugapablo.com> wrote:
On Wed, 07 Dec 2005 12:16:32 -0800, web.dev wrote:

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.


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
Dec 8 '05 #6
On Wed, 07 Dec 2005 12:43:34 -0800, Lee wrote:
So why did you ask how to loop through them?
What are you really looking for?


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

Dec 8 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by manish | last post: by
4 posts views Thread by Bosconian | last post: by
4 posts views Thread by Jean-Christophe Michel | last post: by
2 posts views Thread by Viet | last post: by
14 posts views Thread by Denny | last post: by
1 post views Thread by Tanja Schaettler | last post: by
1 post views Thread by laredotornado | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.