JS Show/Hide Div With Checkbox

Discussion in 'JavaScript' started by timallard, Aug 9, 2010.

  1. #1
    Hello,

    I have a checkbox marked "Yes" which will show a div with the ID of Yes if checked. The problem is, if it is unchecked nothing happens. I think a simple if else statement would do the trick but I am not sure...

    I want to show a div on checked and hide on unchecked. This is my code. all help is greatly appreciated.

    <input name="locationWantMap" type="checkbox" value="Yes" onchange="show('map');show('address')"/>

    i think ther needs to be an - if checked then show, if unchecked then hide etc..but since the code will not change without a refresh i do not know how to go about doing this.

    Tim
     
    timallard, Aug 9, 2010 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi Tim,
    I am not sure, but I guess onchange event doesn't do what you need. Try onclick instead and check inside the show() function if checkbox is checked or not...
    Luk
     
    lp1051, Aug 9, 2010 IP
  3. ChamlingDibya

    ChamlingDibya Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think this is wot you are looking for:
    P.S do inform if it works.

    <html>
    <head>
    <script>
    function fnchecked(blnchecked)
    {
    if(blnchecked)
    {
    document.getElementById("divcheck").style.display = "";
    }
    else
    {
    document.getElementById("divcheck").style.display = "none";
    }

    }
    </script>
    </head>
    <body>
    <input type="checkbox" name="fldcheckbox" id="fldcheckbox" onclick="fnchecked(this.checked);"/>
    <div id="divcheck" style="display:none;">
    this is a test file
    </div>
    </body>
    </html>
     
    ChamlingDibya, Aug 9, 2010 IP
  4. James Bloony

    James Bloony Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi ChamlingDibya,he hasn't replied, but I found your code to do exactly the same thing and it works for me, thanks very much.James
     
    James Bloony, Nov 21, 2012 IP