SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Show & Hide Div

  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Show & Hide Div

    I am trying to open and close a div via a check box. Using the script below the layer appears perfectly. Closing the layer does not function at all. I am not a programmer so I am hoping it is a simple error that someone who knows javascript can figure out. Thanks for any help.

    function showHide(layerName) {
    var divID = document.getElementById(layerName);

    if (divID.style.display = 'none') {
    divID.style.display = 'block'
    } else {
    divID.style.display = 'block'
    }
    }


    <input onclick="showHide('myLayer');" type="checkbox" value="" class="checkBox" />

    <div id="myLayer" style="display:none;">Look at Me!</div>

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change the display property to 'none' in your else clause.
    Code:
    else { 
    divID.style.display = 'none';
    }

  3. #3
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I caught that earlier, should of added it to the post. Made the change but it still doesn't close.

    I changed the else to a if else and that did not work either.

    function showHide(layerName) {
    var divID = document.getElementById(layerName);

    if (divID.style.display = 'none') {
    divID.style.display = 'block'
    } else if (divID.style.display = 'block'){
    divID.style.display = 'none'
    }
    }

    Thanks

  4. #4
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You also need two equals signs in your test for equality.
    Code:
    if (divID.style.display == 'none')

  5. #5
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome, you rock!

    Thanks r51.


Bookmarks

Posting Permissions

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