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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    boolean function to hide/show div

    Hi, I am very new to javascript and html.

    I am trying to create a function which will hide or show a div (div1). Right now, it is always showing the div whether the boolean value is true or false. I know that it is getting inside the else if part, but it doesn't seem to be working.


    Code:
    <script type="text/javascript">
    
    function hideDiv(flag)
    	{
    	if (flag == false)
    		{
    		document.getElementById('div1').style.visibility="visible";
    		}
    	else if (flag == true)
    		{
    		document.getElementById(div1).style.visibility="hidden";
    		}	
    	}
    
    var bool = new Boolean(true)
    hideDiv(bool);
    </script>
    
    <body>
    <div id="div1">
    Text!
    </div>
    </body>
    Thank you for taking a look

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    document.getElementById(div1).style.visibility="hidden";


    Here you are:-

    Code:
    <script type="text/javascript">
    
    function hideDiv(flag) {
    if (flag == false) {  // or if (!flag) {
    document.getElementById("div1").style.visibility="visible";
    }
    else if (flag == true) {  // or just else, or else if (flag) {
    document.getElementById("div1").style.visibility="hidden";
    }	
    }
    
    </script>
    
    <body onload = "hideDiv(false)">
    
    <div id="div1">
    Text!
    </div>

    There is no point in creating the variable bool.


    Quizmaster: On what part of the body is a lobotomy performed?
    Contestant: The bottom.
    Last edited by Philip M; 03-20-2010 at 08:10 PM.

  • #3
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much, works great!

  • #4
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    Quote Originally Posted by popapez View Post
    Thank you very much, works great!
    Wouldn't that function be more useful if it could be used on any element?

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Lightbulb Consider this ...

    Why not make it general purpose so that you might use it here or elsewhere?
    Code:
    <script type="text/javascript">
    function hideDiv(IDS,flag) {
      var sel = document.getElementById(IDS);
      if (flag == false) { sel.style.visibility="visible"; }  // or sel.style.display="block";
                    else { sel.style.visibility="hidden"; }	  // or sel.style.display="none";
    }
    </script>
    
    <body onload = "hideDiv('div1',false);hideDiv('div2',true)">
    
    <div id="div1">Text!</div>
    <div id="div2">More Text!</div>


  •  

    Posting Permissions

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