473,320 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 20929
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: manish | last post by:
I have created a function, it gives more readability compared to the print_r function. As of print_r, it works both for array or single variable. I just want to add in it, the opton to view the...
4
by: Bosconian | last post by:
I've been researching this for hours and can't seem to find the right syntax. I need to retrieve a value of an array by referencing the element using a string variable. For example: $data...
4
by: Jean-Christophe Michel | last post by:
Hi, In a complex merging of two (non ordered) xml files i need to keep track of the elements of the second tree that were already merged with first tree, to copy only unused elements at the end....
2
by: Viet | last post by:
I have problem in retrieve a value of a form element that have name begin with a number. For example, for this HTML code <input type="text" name="0000ABC12345CD6789" id="0000ABC12345CD6789"> I got...
14
by: Denny | last post by:
For most of my variable names, I use Hungarian notation to determine between one and the other. But what names can I use for public and private variables? I was using prv_varName and pub_varName...
1
by: Tanja Schaettler | last post by:
Hello! I have an existing SOAP message which looks like: <?xml version="1.0" encoding="UTF-8" ?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"...
5
by: johanneskrueger | last post by:
Hello, I'm currently using <xsl:copy-of select="document(...)/svg:svg"/to embed an SVG file into an XHTML file. I already defined the SVG namespace and assigned svg as its prefix in my XSLT 1.0...
1
by: laredotornado | last post by:
Hi, I'm using php 4.4.4. Given a particular directory, I want to delete all the files that begin with the variable "$prefix". What is the simplest way to do this? Thanks ,- Dave
1
by: mrityunjay11 | last post by:
the code is as #!/bin/sh i=1; while do echo "welcome $i times"; i= 'expr $i + 1 '; done
7
by: rmurgia | last post by:
There is a sequence of ProdCode fields; in this case 1 - 3. I would like to populate the strCBFilter variable with the values contained in those fields using a loop: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.