Closed Thread Icon

Preserved Topic: how to set a state on several elements at the same time? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=18297" title="Pages that link to Preserved Topic: how to set a state on several elements at the same time? (Page 1 of 1)" rel="nofollow" >Preserved Topic: how to set a state on several elements at the same time? <span class="small">(Page 1 of 1)</span>\

 
DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 05-24-2002 23:14

Right now I'm working on a dhtml-presentaion-type-site for our team that got canned from work.
Not ready for show yet.

Is there a good way to loop through all elements with an "ID"?

I need to check position & visibility on each and every element as I click a link to show one element.
Right now I'm calling a "show()" for one element and a "hide()" for each and everyone of the others. I'm talking about 15-20 scripted elements here that I need to check on every click... I also have two different states the element can be in, so I'll have to catch each element, perform a test on it, do an action, then move to the next element to be checked.

'gotta be an easier way...
What I'm thinking is to call one function that will loop through all elements on the page and for each one test for the values i need to test, but how do I loop through all the elements in a crossbrowser fashion...

//Dan

{cell 260}
-{ a vibration is a movement that doesn't know which way to go }-

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 05-24-2002 23:47

Give the elements IDs like

element0
element1
element2
...
element9
element10
element11

etc. Then you'll need a JS variable called something like numberOfElements. Then you can just do this:

for (a=0; a < numberOfElements; a++)
{
document.getElementById('element' + a).style.visibility = 'hidden';
}

DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 05-25-2002 00:45

Perfect Slime, thanx!
I actually just solved it myself using that principle
(I was hoping that I wouldn't have to rename all my ID's though...)

I did rename all id's in groups based on information levels "info0, info1, info2, phase0, phase1..."
Then I did this:

code:
function hideAll(name){
var nest = "";
var i=0;
var allElements=new Array();
if(document.getElementById){
while (document.getElementById(name+i)!=null){
allElements[i]= document.getElementById(name+i);
i++;
}
} else if(document.all){
while (document.all[name+i]!=null){
allElements[i]= document.all[name+i];
i++;
}
} else if(document.layers){
while (document.layers[name+i]!=null){
allElements[i]= document.layers[name+i];
i++;
}
}
for(y=0;y < allElements.length; y++){
if(document.layers){
temp = allElements[y].left;
} else {
temp = parseInt(allElements[y].style.left);
}
if(temp > 0){
layerName = name+y;
moveLeft(layerName,nest);
temp = "";
}
}
}


This takes care of all ID's in that group that is "on screen" and are to be scrolled left of screen. I'll modify it for the visibility and the scroll up parts where needed too.

Now for the fun part... This is supposed to be fully crossbrowser... it works with IE, Opera, mozilla, but NS4... Damned what a piece of sh*t that is!
After a lot of trickery, the above actually works!
Wooot!

Next Q, is it possible to detect how many parameters the function recieves, if I want to pass more than one group to it, not knowing how many at this point?

//Dan


{cell 260}
-{ a vibration is a movement that doesn't know which way to go }-

Hew
Neurotic (0) Inmate
Newly admitted
posted posted 05-25-2002 01:28

MMh, I dont know myself, I would have done it the above way too , but does anyone know if theres an array of all the elements, like there is with images and forms .. you know the way you can use:
form[3]
and
images[4]
or frame[6], that kind of thing ? this could kinda be handy for something Im doing at the momment too


mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 05-25-2002 09:14

Hew, document.getElementsByTagName("tagName") will return an array of all elements that match specified tag.

Example:

layers = document.getElementsByTagName("div");
laters[3].style.backgroundColor = "#c0c0c0";

BTW Note that getElementsByTagName is not yet supported by all web browsers.




[This message has been edited by mr.maX (edited 05-26-2002).]

Hew
Neurotic (0) Inmate
Newly admitted
posted posted 05-25-2002 17:24

Thanks again MrMax !, there should be a tag for you like [mrmax=thanks]ample thanks [/mrmax] you help so much it would save a lot of people a lot of time

Is it rude to ask people questions on other people posts ? Well I guess its quite on topic , but anywho ..

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 05-25-2002 22:56

I thought it was getElementsByTagName. I may very well be wrong.

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 05-26-2002 09:20

Oops! Shit happens when you write in a hurry.

Anyway, Slime, you're right, it should be getElementsByTagName (and not getElementsByTag like I wrote above). I'll edit my post from above to avoid confusion...


« BackwardsOnwards »

Show Forum Drop Down Menu