Skip to content
Advertisement

Prevent checkbox from unchecking when clicked (without disable or readonly)

I’m using a checkbox and I want people to be able to check/uncheck it. However, when they uncheck it, I’m using a jQueryUI modal popup to confirm that they really want to do that. Thus they can click OK or Cancel, and I want my checkbox to be unchecked only if they click OK.
That’s why I would like to catch the uncheck event to prevent the checkbox from being visually unchecked, and uncheck it myself programmatically if the user happens to click on OK.

How could I do that ?

PS: I know I could re-check it after if the user clicks on Cancel but that would trigger the check event which I do not want.

Advertisement

Answer

$("#checkboxID").on("click", function (e) {
    var checkbox = $(this);
    if (checkbox.is(":checked")) {
        // do the confirmation thing here
        e.preventDefault();
        return false;
    }
});
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement