Here is the HTML:
<ul>
<li>
<div class="checkbox-wrap">
<p>Check this:
<input type="checkbox" />
</p>
</div>
</li>
<li>
<div class="checkbox-wrap">
<p>Check this:
<input type="checkbox" />
</p>
</div>
</li>
<li>
<div class="checkbox-wrap">
<p>Check this:
<input type="checkbox" />
</p>
</div>
</li>
</ul>
The jQuery:
$this('input:checkbox').change(function(){
if($(this).is(":checked")) {
$('div.checkbox-wrap').addClass("active-checkbox");
} else {
$('div.checkbox-wrap').removeClass("active-checkbox");
}
});
When you check a checkbox the class “active-checkbox” is being added to the whole 3 divs.
Is there any way to add the class for a single div and not for the whole 3 divs without using ID or custom class names ?
Thanks
You have wrongly put
$this('input:checkbox')instead of$('input:checkbox'), you can use closest() to find the first occurance of selector in up in ancestor hierarchy.Live Demo