I have the following jQuery script to transform a checkbox into a jQuery-UI button and set up the icons to be displayed. It also changes the icon when the button is clicked. It all works fine. However I cannot seem to hit on the correct syntax to set the initial icon based on the initial checked state of the checkbox.
function setCheckBoxImages() {
$(".check-box-image").button({
icons: { primary: 'ui-icon-check' },
text: false
}).change(function () {
$(this).button("option", {
icons: { primary: this.checked ? 'ui-icon-check' : '' }
});
});
};
<input class="check-box-image" type="checkbox" checked="@item.IsBusinessHours" id="checkbox1" disabled/><label for="checkbox1"></label>
EDIT: Here is my final working markup and script as per accidentalgenius answer below:
@Html.CheckBox(@cbMobileId, @item.IsMobile, new { @class = "check-box-image", disabled = "disabled" })<label for="@cbMobileId">
function setCheckBoxImages() {
var checkboxes= $(".check-box-image");
checkboxes.button({
text: false
}).change(function () {
$(this).button("option", {
icons: { primary: this.checked ? 'ui-icon-check' : 'ui-icon-minus' }
});
});
checkboxes.each(function () {
$(this).change();
});
};