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

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

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
"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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.