Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  164] [ 2]  / answers: 1 / hits: 39763  / 13 Years ago, thu, november 3, 2011, 12:00:00

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?


More From » jquery

 Answers
22

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().


[#89316] Tuesday, November 1, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
diontajm

Total Points: 391
Total Questions: 104
Total Answers: 104

Location: Netherlands
Member since Mon, Jun 7, 2021
3 Years ago
;