Rajesh Kumar
Rajesh Kumar

Reputation: 79

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.

Upvotes: 1

Views: 3213

Answers (2)

Martin Parenteau
Martin Parenteau

Reputation: 73751

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);
    }
});

Upvotes: 1

Angel ofDemons
Angel ofDemons

Reputation: 1267

I've made a little sample of when the user deselects the checkbox. Hope this is what you're looking for. Goodluck!

$('#check').on('click', function(){
if(!$(this).is(':checked')) {
    // do something if the checkbox is NOT checked
    alert(this.id);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Check<input type="checkbox" id="check"/>

Upvotes: 2

Related Questions