I am trying to change the class of a button when a checkbox is checked but I’m having trouble getting it right. When the checkbox is selected I want to change the buttons class to buttonsshow from buttonshide.
HTML
<div class="checkbox">
<input id="ChartstoDisplayAll" type="checkbox" name="ctl00$MainContent$ChartstoDisplayAll" /><label for="ChartstoDisplayAll">Select All</label>
<p class="checkbox" style="margin-left:5px;"> or select individually below </p>
<hr class="horizline" style="width:870px;"/>
</div>
<div id="buttons" class="buttonshide">
<input type="image" name="ctl00$MainContent$btnGetChart" id="MainContent_btnGetChart" title="Click here to retrieve the charts" Text="Build the Charts" src="Styles/Images/stats.png" alt="Build the Charts" align="left" />
<p style="font-size:.8em; font-weight:bold;">Build Charts</p>
</div>
FYI…There is other html between these two divs. I took it out for brevity as none have an id of buttons.
Here is the javascript. (the latest way that I’ve tried)
//this script will change the style of the "Select All" checkbox and make visible the "Build Chart" button
$(document).ready(function () {
$('#ChartstoDisplayAll').click(
function () {
$("INPUT[type='checkbox']").attr('checked', $('#ChartstoDisplayAll').is(':checked'));
$(this).next().toggleClass("selected");
$(this).find("buttons").toggleclass("buttonsshow");
});
});
Here is the fiddle link
http://jsfiddle.net/dinotom/h25tM/2/
Here is the completed, working script which changes the text color and shows the button when the checkbox is selected.
//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');
} else {
$('#buttons').addClass("buttonshide").removeClass('buttonsshow');
$(this).next().removeClass("selected");
}
});
});
Try this:
Also, you should use the change event instead of click