...

View Full Version : hide many div layers...?



homerUK
06-22-2004, 05:24 PM
I've got a load of DIV layers that I hide in a function... is there a quicker way of doing it?? I was hoping there might be a way of saying ALL divs - hide...?

here's what happens at the mo



document.all['test1'].style.display = "none";
document.all['test2'].style.display = "none";
document.all['test3'].style.display = "none";=


maybe I can name them all "__div_" then put the name of the div....... could I then say "any DIVs which are visible that start with "__div_" = hide them...?

any ideas?

Roy Sinclair
06-22-2004, 06:15 PM
If you make all the DIVs members of a class (<div class="showOrHide">) you can alter the class instead of the individual DIVs and thus affect all the divs using that class at the same time.

See this thread for the straight up on how to alter the CSS class: http://codingforums.com/showthread.php?s=&threadid=14259

homerUK
06-23-2004, 10:20 AM
cheers roy... I didnt want to change the class of the DIVs... so I used the example you gave me with a few tweaks...



divAry = document.getElementsByTagName("div");
divLen=divAry.length;
for(j=0;j<divLen;j++){
if (divAry[j].style.display == "block") {
divAry[j].style.display = "none";
}
}


basically I get all the Divs which are on the page, and if any are visable, I hide them....
the problem is that if the user has any of their own DIVs on the page, as well as my code.... it will hide theirs!!

I might need to give mine a name prefix and do a check on that? you think that's best?

Roy Sinclair
06-23-2004, 04:59 PM
Since the use of DIV tags is increasing by leaps and bounds, I'd say you definitely need to use some way of making the divs you are scripting actions on distinct from any other divs on the page. May I ask why you can't add a class to those divs though?

homerUK
06-23-2004, 07:04 PM
May I ask why you can't add a class to those divs though?

the divs already have a class associated with them, and I think if I start changing them they'll go potty on me!! :confused:

Will post the code once I have it working for the other bit...!

Roy Sinclair
06-23-2004, 07:54 PM
If they already have a class you should be able to use that class unless it's also used by items you don't want to disappear. If that's the case you can add a class to those divs (multiple CSS classes are allowed: class="firstclass secondclass thirdclass steerage").

homerUK
06-25-2004, 01:11 PM
cheers for the help guys... I am gonna try to apply a class.. but I also was wondering how to change this code



divAry = document.getElementsByTagName("div");
divLen=divAry.length;
for(j=0;j<divLen;j++){
if (divAry[j].style.display == "block") {
divAry[j].style.display = "none";
}
}


to ONLY effect the DIVs which start with "tr_" - I guess it is something like substring move 3 chars and compare it to "tr_" - but I cant find the code to do it!!

anyone else know it?

Willy Duitt
06-25-2004, 01:24 PM
Try something like this. I don't know if I have the condition in the right place but the match will work.


divAry = document.getElementsByTagName("div");
divLen=divAry.length;
for(j=0;j<divLen;j++){
if (divAry[j].id.match(/^(tr_)/gi) && divAry[j].style.display == "block") {
divAry[j].style.display = "none";
}
}


Edit ooops, forgot the id.... :p

homerUK
06-25-2004, 01:34 PM
worked perfectly - many thanks.... :D :thumbsup:

Willy Duitt
06-25-2004, 01:37 PM
Your welcome :D

BTW: I wrote that to be case insensitive both TR_ and tr_ will pass the match. If that is not what you want remove the case insensitive flag (i) from the end of the expression....

homerUK
06-25-2004, 01:40 PM
BTW: I wrote that to be case insensitive both TR_ and tr_ will pass the match. If that is not what you want remove the case insensitive flag (i) from the end of the expression....

fan-blummin-tastic! :D :D cheers!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum