ok. As you can see. I have some inefficient code.
The point of it is to check the children of a parent element.
I couldn’t figure out how to use .children(), so I wrote some longhanded code keying on 2 fields (the ID and class). I’d rather just use the class but I couldn’t figure out how to use just the “Level” class by itself.
But I don’t want to depend on the ID, just the class. (plus I’d rather use .children() if anyone know how)
extra points for making the JQuery shorter, as I don’t think I need the IF statements either.
<div class="jobtypeHeadLghtRed inputFields" id="jobTypeName">
<input type="checkbox" class="checkbox Level1" checked="'checked'" id="IsSelected_J_L1">
<div class="sectionHeaderText" id="JobType">level 1 item</div>
</div>
<div class="facilityHeadLghtBlue inputFields" id="jobTypeName">
<input type="checkbox" class="checkbox Level2" checked="'checked'" id="IsSelected_J_L2">
<div class="sectionHeaderText" id="JobType">level 2 item</div>
</div>
<div class="HeadLghtGreen inputFields" id="jobTypeName">
<input type="checkbox" class="checkbox Level3" checked="'checked'" id="IsSelected_J_L3">
<div class="sectionHeaderText" id="JobType">Level 3 item</div>
</div>
$(function() {
$(":checkbox").change(function() {
var id = this.id;
var level = id.substring(id.length - 1);
if(level == 1){
$('[class*="Level2"], [class*="Level3"]').attr('checked', this.checked);
}
if(level == 2){
$('[class*="Level3"]').attr('checked', this.checked);
}
});
});
This will work
And here’s the fiddle http://jsfiddle.net/aU9AL/
And forget about .children(). This is not the case.