www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Simple if else not working. Please help!

  1. #1
    Join Date
    Jun 2008
    Posts
    207

    resolved [RESOLVED] Simple if else not working. Please help!

    Hi, Can anyone tell me why this is not working? I'm just trying to get a div to be made visible or hidden at the click of a link.

    <script language="JavaScript">
    function showhide()
    {
    var testvar = document.getElementById("mydiv").style.visibility
    if (testvar = "visible")
    {
    document.getElementById("mydiv").style.visibility = "hidden";
    }
    else
    {
    document.getElementById("mydiv").style.visibility = "visible";
    }
    }
    </script>


    <div id="mydiv" style="visibility:visible;">
    Blah blah blah...
    </div>

    <a href="javascript:showhide()">Click here to enable the element<a/>

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    compare:
    Code:
    if (testvar == "visible")
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Jun 2008
    Posts
    207
    Thank you, and i'm a little embarrassed...

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    <script  type="text/javascript">
    function showhide()
    {
    var testvar = document.getElementById("mydiv");
    testvar.style.visibility=(testvar.style.visibility == "visible")? "hidden" : "visible";
    }
    </script>
    At least 98% of internet users' DNA is identical to that of chimpanzees

  5. #5
    Join Date
    Jun 2008
    Posts
    207
    I have a problem: The first click on the checkbox doesn't change the visibility. You need to click twice for it to start working. Is there anyway to change this?

    <script language="JavaScript">
    function showhide()
    {
    var testvar = document.getElementById("mydiv").style.visibility
    if (testvar == "visible")
    {
    document.getElementById("mydiv").style.visibility = "hidden";
    }
    else
    {
    document.getElementById("mydiv").style.visibility = "visible";
    }
    }
    </script>

    <form>
    <input type="checkbox" id="show" onclick="showhide()" checked="checked"> Yes please send me an SMS reminder of the event<br>
    </form>


    <div id="mydiv">
    Mobile number form field
    </div>

  6. #6
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    It must be explicitly set in the div, else the visibility property returns "" empty:
    Code:
    <div id="mydiv" style="visibility:visible;">
    At least 98% of internet users' DNA is identical to that of chimpanzees

  7. #7
    Join Date
    Jun 2008
    Posts
    207
    Thanks Fang. You legend.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles