[SOLVED] Javascript - Find all matching ID's starting with..

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

[SOLVED] Javascript - Find all matching ID's starting with..

Post by phice »

I have a list of id's that all start with "list_" and a given number.

Code: Select all

<div id=&quote;list_1&quote;>jkfd</div>
<div id=&quote;list_2&quote;>jkfd</div>
<div id=&quote;list_3&quote;>jkfd</div>
<div id=&quote;list_4&quote;>jkfd</div>
<div id=&quote;list_5&quote;>jkfd</div>
<div id=&quote;list_6&quote;>jkfd</div>
<div id=&quote;list_7&quote;>jkfd</div>
<a href=&quote;#&quote; onclick=&quote;javascript:togglesweep( 'list_' );&quote;>Toggle display</a>
I want to create a function in javascript that will find all of those elements and reverse the value of .style.display (from none to block, and vice versa). I currently have a function that will set all elements that are from id="list_1" to id="list_100" but I don't want to limit it to 100 and I dont want it to freeze the browser for x amount of seconds during this call.

Any suggestions are welcome.
Last edited by phice on Tue Jun 28, 2005 2:39 am, edited 1 time in total.
Image Image
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

how about just stopping when you found an element which doesn't exist?

(or keep a list of which elements does exist)

or you could give them all let's say tag="TNT" and just loop through the children of a container, and checking for that tag, however doesn't work if they are spread out.

but any of those 3 should be sufficient?
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Figured it out.

I set all of the rows as the same class and then used getElementsByClassName('classname');

I really need to get some kind of chart for these javascript functions I don't know about.
Image Image
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

If you know the name is list_x where x is an integer you should be able to just use a while loop. Example (my javascript is not 100% as at present I am think php:wink: but should give you a starting point).

Code: Select all

var elem;
var counter=1;
while (elem=getElementById('list_'+counter)) {
  // Do whatever;
  counter++;
}
Post Reply