7

I have a checkbox:

<input type="checkbox" name="mycheckbox" id="mycheckbox" value="0" />
<div style="display:none">
This content should appear when the checkbox is checked
</div>

Does anyone have a simple way to do this?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Satch3000
  • 47,356
  • 86
  • 216
  • 346

8 Answers8

21

This will show it when the checkbox is checked, and hide it again when the checkbox is unchecked:

$('#mycheckbox').change(function() {
    $(this).next('div').toggle();
});

...although it would be better if you'd assign that DIV an id, so it could be selected more quickly:

<input type="checkbox" name="mycheckbox" id="mycheckbox" value="0" />
<div id="mycheckboxdiv" style="display:none">
    This content should appear when the checkbox is checked
</div>

<script type="text/javascript">
$('#mycheckbox').change(function() {
    $('#mycheckboxdiv').toggle();
});
</script>

http://jsfiddle.net/mblase75/pTA3Y/

If you want to show the div without hiding it again, replace .toggle() with .show().

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
  • Why does the script have to be after the content? – rlab Apr 16 '13 at 18:56
  • 1
    Because I didn't use `$(document).ready`. If you do, then it doesn't. – Blazemonger Apr 16 '13 at 18:56
  • How would you fix the problem I have with this function? If I start with div visible, checking the checkbox makes div disappear and unchecking makes it appear again. – rlab Apr 16 '13 at 19:23
  • This isn't a JS issue. Either start with the div hidden, or start with the box checked. – Blazemonger Apr 16 '13 at 19:55
  • So the script doesn't actually see the checkbox state? It just notices the change? – rlab Apr 16 '13 at 20:47
  • 1
    That's what `.toggle()` does, yes. If you want it to reflect the checkbox's exact state (checked=visible, unchecked=hidden), try: `$('#mycheckboxdiv').toggle($(this).is(':checked'));` and make sure the initial states match. – Blazemonger Apr 16 '13 at 21:24
  • Please answer this! How can you make this method work with more than one checkbox? – MaxwellLynn Jul 28 '13 at 16:53
  • anyone can give a try to [this](http://stackoverflow.com/a/18307407/2218697) too, it worked fine for me, hope helps. – Shaiju T Apr 13 '15 at 12:15
6

Attach a change event, and check whether a checkbox is checked or not. If the checkbox is checked, show the div. Otherwise, hide it:

$('#mycheckbox').change(function(){
    if(this.checked) {
        $(this).next().show();
    } else {
        $(this).next().hide();
    }
});

You should also have a look at the jQuery docs, before asking such a trivial question.

Rob W
  • 341,306
  • 83
  • 791
  • 678
3
$("#mycheckbox").change(function(){ 
    $(this).next().toggle(this.checked); 
});
jrummell
  • 42,637
  • 17
  • 112
  • 171
1

If you are looking for only css solution, then this can help you.

#toggle-content{
display:none;
}
#mycheckbox:checked ~ #toggle-content{
  display:block;
  height:100px;

}

Fiddle

vijayscode
  • 1,905
  • 4
  • 21
  • 37
0

I got it working with

  $(document).on('ready',function() {
    $('#checkbox').on('change',function(){
        if(this.checked) {
            console.log("checked");
        } else {
            console.log("unchecked");
        }
    })
});
Spinstaz
  • 287
  • 6
  • 12
0
$("#mycheckbox").click(function() { $(this).next("div").toggle() })
jessegavin
  • 74,067
  • 28
  • 136
  • 164
0
$("input#mycheckbox").click(function() {
    if($(this).is(":checked") {
        $("div").slideDown();
    }
    else {
        $("div").slideUp();
    }
})
Manuel van Rijn
  • 10,170
  • 1
  • 29
  • 52
0
if ($("#mycheckbox").checked) {
   $("div").style.display = "block";
}
L3viathan
  • 26,748
  • 2
  • 58
  • 81