Given the following HTML output from my aspx page, which displays a checkboxlist;
<div id="selectContainer" class="wrapper" >
<div id="cbxArea" class="checkbox">
<table id="ChartstoDisplay" style="border-color:DimGray;border-width:2px;border-style:Solid;width:300px;">
<tr>
<td><input id="ChartstoDisplay_0" type="checkbox" name="ctl00$MainContent$ChartstoDisplay$ChartstoDisplay_0" value="Selected Year Cumulative P/L" /><label for="ChartstoDisplay_0">Selected Year Cumulative P/L</label></td>
</tr><tr>
<td><input id="ChartstoDisplay_1" type="checkbox" name="ctl00$MainContent$ChartstoDisplay$ChartstoDisplay_1" value="Month by Month P/L for selected year" /><label for="ChartstoDisplay_1">Month by Month P/L for selected year</label></td>
</tr><tr>
<td><input id="ChartstoDisplay_2" type="checkbox" name="ctl00$MainContent$ChartstoDisplay$ChartstoDisplay_2" value="All Commodities P/L for selected year" /><label for="ChartstoDisplay_2">All Commodities P/L for selected year</label></td>
</tr><tr>
<td><input id="ChartstoDisplay_3" type="checkbox" name="ctl00$MainContent$ChartstoDisplay$ChartstoDisplay_3" value="Current Month's P/L by Commodity" /><label for="ChartstoDisplay_3">Current Month's P/L by Commodity</label></td>
</tr><tr>
</div>
</div>
I am trying to change the class of the individual item when it is selected. I am having trouble getting my jquery to find the right object to change the class of. Here is my latest iteration.
$('#ChartstoDisplay').change(
function () {
$('#ChartstoDisplay').each(function () {
$(this).is(':checked').$('label').toggleclass("selected");
});
});
I also tried this way
$('#ChartstoDisplay').change(
function () {
$('#ChartstoDisplay').each(function () {
$(this).is(':checked').$('label').attr.add('style',"color:darkgreen;");
});
});
Here is the completed script working in it’s entirety
//this script will change the style of the "Select All" checkbox and make visible the "Build Chart" button
$(document).ready(function () {
$('#ChartstoDisplayAll').change(
function () {
if ($('#ChartstoDisplayAll').is(':checked')) {
$(this).next().addClass("selected");
$('#buttons').addClass("buttonsshow").removeClass('buttonshide');
$("#cbxArea input[type=checkbox]").each(function () {
$(this).prop("checked", true);
$(this).siblings("label").toggleClass("selected");
});
} else {
$('#buttons').addClass("buttonshide").removeClass('buttonsshow');
$(this).next().removeClass("selected");
$("#cbxArea input[type=checkbox]").each(function () {
$(this).prop("checked", false);
$(this).siblings("label").removeClass("selected");
});
}
});
});
try this