Desired Behaviour
On changing the checked status of a checkbox, I want to remove or add a class.
I have the functionality for the logic working, but am having trouble traversing and targeting a specific li
and making style changes.
HTML
<div class="one">
<div class="two">
<p>31</p>
<div class="three">
<table>
<tbody>
<tr>
<td>
<input type="checkbox" class="css-checkbox" id="checkboxG1" name="checkboxG1">
<label class="css-label" for="checkboxG1">area_01 - click me</label>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="four">
<ul>
<li class="area_01 common"></li>
<li class="area_02 common"></li>
<li class="area_03 common"></li>
<li class="area_04 common"></li>
<li class="area_05 common"></li>
<li class="area_06 common"></li>
</ul>
</div>
</div>
jQuery
The following is my last attempt after a series of failed attempts using next()
.
I'm either messing up my traversing, or I'm not using removeClass()
correctly.
$("input.css-checkbox:checkbox").change(function () {
if (this.checked) {
// remove a class
//var myVar = $(this).next($("li.area_01"));
var myVar = $(this).parent().parent().parent().find(".four").find("li.area_01");
myVar.removeClass("common");
} else if (!this.checked) {
// add a class
var myVar = $(this).parent().parent().parent().find(".four").find("li.area_01");
myVar.addClass("common");
}
});
jsFiddle
http://jsfiddle.net/rwone/fns87knc/
Update: I should add that there will be a number of lists with the same classes, and therefore I need to specifically target "the next instance of this class from where I am clicking".