I have a lot of listst with checkboxes like so:
$(".filteritem").on('change', function(){
$(this).closest('label').toggleClass('highlight');
});
FIDDLE
HTML:
<ul class="checkboxlist">
<li><label><input type="checkbox" id="1"> Lorem Ipsum</label></li>
<li><label><input type="checkbox" id="223"> Lorem Ipsum</label></li>
<li><label><input type="checkbox" id="32"> Lorem Ipsum</label></li>
</ul>
JavaScript:
$( '.checkboxlist' ).on( 'click', 'input:checkbox', function () {
$( this ).parent().toggleClass( 'highlight', this.checked );
});
Live demo: http://jsfiddle.net/MGVHX/1/
Notice that I use event delegation, instead of binding the same handler to every check-box.
You are currently trying to call toggleClass
on the input
element, not the label
. You can use parent to get the label
:
$(".filteritem").click(function(){
$(this).parent().toggleClass('highlight');
});