Visibility With Divs

Discussion in 'JavaScript' started by wierdo, Mar 1, 2008.

  1. #1
    I was trying to make a script that when you click an image, a div is made hidden or visible. I'm not really sure how to describe it other than that, so here's the code.

    <script language="JavaScript">
    visibility = visible;
    	function menu(whichMenu){
    		if (document.getElementById)
    {if (document.getElementById(whichMenu).style.visibility == visible){
    whatState = hidden;
    } 
    
    else {whatState = visible;
    }
    document.getElementById(whichMenu).style.visibility = whatState;
    }
    	
    	else {
    
    if (document[whichMenu].visibility == visible){
    whatState = hidden;
    }
    
     else {whatState = visible;
    }
    document[whichMenu.visibility = whatState;
    }
    
    	}
    	</script>
    Code (markup):
    The image is just simply:

    <img onClick="menu('leftcontent1')" ...
    Code (markup):
    Can someone tell me why this isn't working? Thanks!
     
    wierdo, Mar 1, 2008 IP
  2. Morishani

    Morishani Peon

    Messages:
    239
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    visible, hidden should be a string and not a variable.
    if you want you can set a variable in the global scope that says :
    
    var visible = "visible";
    var hidden = "hidden";
    
    Code (markup):
    good luck :)
     
    Morishani, Mar 2, 2008 IP
  3. lephron

    lephron Active Member

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #3
    You've seriously overcomplicated this! All you need is a function like:

    function toogle(id)
    {
    var elem = document.getElementById(id);
    if(!elem) return;

    if(elem.style.visibility == 'hidden') elem.style.visibility = 'visible';
    else elem.style.visibility = 'hidden';
    }

    <img onClick=toggle('leftcontent1') ...
     
    lephron, Mar 2, 2008 IP