Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post

    hide many div layers...?

    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

    Code:
    	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?

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.p...threadid=14259
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    cheers roy... I didnt want to change the class of the DIVs... so I used the example you gave me with a few tweaks...

    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";
    		}
    	}
    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?

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Roy Sinclair
    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!!

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

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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").
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #7
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    cheers for the help guys... I am gonna try to apply a class.. but I also was wondering how to change this code

    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?

  • #8
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try something like this. I don't know if I have the condition in the right place but the match will work.

    Code:
    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....
    Last edited by Willy Duitt; 06-25-2004 at 01:29 PM.

  • #9
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    worked perfectly - many thanks....

  • #10
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Your welcome

    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....

  • #11
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Willy Duitt
    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! cheers!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •