-3

I found this script where one checkbox (once checked) shows the corresponding div. When Checkbox is Checked Expand Div

But I have multiple checkboxes, which shows its corresponding div once checked.

I tried to add the following to the html file but that didn't work:

<div style="clear: both;"></div>

<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>
Community
  • 1
  • 1
MK69
  • 57
  • 6

3 Answers3

2

Just simply use CSS :checked property for this:

input + div {
  display: none;
}
input:checked + div {
  display: block;
}
<input type="checkbox" name="mycheckbox" id="mycheckbox" value="0" />
<div id="mycheckboxdiv">
  This content should appear when the checkbox is checked
</div>
<input type="checkbox" name="mycheckbox1" id="mycheckbox1" value="0" />
<div id="mycheckboxdiv1">
  This content should appear when the checkbox is checked
</div>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0
    <input type="checkbox" name="mycheckbox" id="mycheckbox" value="0" data-divid="mycheckboxdiv"/>
    <div id="mycheckboxdiv" style="display:none">
    This content should appear when the checkbox is checked
    </div>

    //js code

    $(document).body(function(){
     $("input[type='text']").change(function(){
        var val=$(this).attr('data-divid');
       if($(this).attr('checked')){
         $("div [id='"+val+"']").show();}
       else{
         $("div [id='"+val+"']").hide();}        
     });
    });
Muhammad Atif
  • 1,050
  • 1
  • 9
  • 21
0

Another solution is to wrap your items in a container div and when user checks the check box,use closest() to get the container div,then use find to get the associated text div and show/hide it.

I prefer using closest() and find() than next() method because the first one will work even if you add some new element in between your checkbox and the associated div in the future.

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

And your jQuery code to handle the checkcbox checcked event

$(function(){

  $("input[name='mycheckbox']").change(function(e){

     var _this=$(this);
     if(_this.prop("checked"))
     {
        _this.closest(".itemContainer").find(".meta").show();
     }
     else
     {
         _this.closest(".itemContainer").find(".meta").hide();
     }
  });

});

Here is a working sample

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • I like your script but it works only for all checkboxes named "mycheckbox".... The checkboxes in my application are named "compid.$thistag_id" where $thistag_id is a variable. How can I change the script as such that it works with all checkboxes starting with the name: compid (whatever the variable is) – MK69 Dec 31 '15 at 18:20
  • You can do a starts with jQuery library Take a look http://jsbin.com/tiqiga/edit?html,js,console,output – Shyju Dec 31 '15 at 18:44