I have tried to follow the specs on jquery documentation but i cannot initialize the label of the button programmatically and add/remove classes to it.
Please have a look at the few lines in this jsbin.
http://jsbin.com/akinod/2/edit
HTML
<label for="xAxisToggleBtn"></label>
<input type="checkbox" id="xAxisToggleBtn"/>
CSS
.on {
background-color: green;
color: white;
}
.off {
background-color:brown;
color: white;
}
Javascript
$(window).load(function() {
$("#xAxisToggleBtn").button();
$('#xAxisToggleBtn').die('click').live('click', function(){
if($(this).is(':checked')){
$(this).button('option', 'label', 'Turn Off');
//why I cannot add/remove classes to change the theme?
$(this).addClass('on');
$(this).removeClass('off');
} else {
$(this).button('option', 'label', 'Turn On');
//why I cannot initialize add/remove classes to change the theme?
$(this).addClass('off');
$(this).removeClass('on');
}
});
});
//why I cannot initialize the label here?
$("#xAxisToggleBtn").button('option', 'label', 'Turn Off');
$("#xAxisToggleBtn").attr('checked', true);
$("#xAxisToggleBtn").addClass('on');
You have to add the class to the button widget, your styles will need to overwrite jQuery-UI own, and make sure the elements are loaded before you try to manipulate them.
DEMO