I have a lot of listst with checkboxes like so:
<ul>
<li><label class="highlight"><input type="checkbox" id="1" class="filteritem">Lorem Ipsum</label</li>
<li><label class="highlight"><input type="checkbox" id="223" class="filteritem">Lorem Ipsum</label</li>
<li><label class="highlight"><input type="checkbox" id="32" class="filteritem">Lorem Ipsum</label</li>
<li><label class="highlight"><input type="checkbox" id="42" class="filteritem">Lorem Ipsum</label</li>
<li><label class="highlight"><input type="checkbox" id="54" class="filteritem">Lorem Ipsum</label</li>
</ul>
<ul>
<li><label class="highlight"><input type="checkbox" id="43" class="filteritem">Lorem Ipsum</label</li>
<li><label class="highlight"><input type="checkbox" id="343" class="filteritem">Lorem Ipsum</label</li>
<li><label class="highlight"><input type="checkbox" id="342" class="filteritem">Lorem Ipsum</label</li>
<li><label class="highlight"><input type="checkbox" id="53" class="filteritem">Lorem Ipsum</label</li>
<li><label class="highlight"><input type="checkbox" id="55" class="filteritem">Lorem Ipsum</label</li>
</ul>
Every checkbox has an unique ID
I want to toggle the background-color of the label when it is checkt.
This is what i got but it does not work:
jQuery:
$(".filteritem").click(function(){
$(this).toggleClass('highlight');
});
CSS:
.highlight {
//change something
}
HTML:
JavaScript:
Live demo: http://jsfiddle.net/MGVHX/1/
Notice that I use event delegation, instead of binding the same handler to every check-box.