Rajesh Kumar Rajesh Kumar - 6 months ago 41
jQuery Question

How to show alert/confirmation only on uncheck of check box

I have a

asp:CheckBoxList
and I want to show a warning message only on uncheck of check box using jquery.

$('.chklist').click(
function () {
if ($(this).is(":not(:checked)")) {
alert(this.id);
}

});


But the above code is always giving
true
even if item is checked or unchecked. As it is taking id for the whole list and not that particular check box. I don't want to implement the things on post back event.

Answer

You should set the click event handler on the input elements of the CheckBoxList, instead of the list itself. Assuming that the CheckBoxList has CssClass="chklist":

$('.chklist input').click(function () {
    if (!$(this).is(':checked')) {
        alert(this.id);
    }
});

It also works with your original code:

$('.chklist input').click(function () {
    if ($(this).is(":not(:checked)")) {
        alert(this.id);
    }
});