JQuery, How to get checkbox unchecked event checkbox and checkbox?

I want to get the value checkboxusing jQuery when to uncheck the checkbox and show this unchecked value in the popup. I tried the code below, but it does not work.

$("#countries input:checkbox:not(:checked)").click(function(){
    var val = $(this).val();
    alert('uncheckd' + val);
}); 

Is it possible to get an uncontrolled value this way?

+13
source share
3 answers

Your selector attaches the event only to the element selected at the beginning. You need to determine the uncontrolled state when changing the value:

$("#countries input:checkbox").change(function() {
    var ischecked= $(this).is(':checked');
    if(!ischecked)
    alert('uncheckd ' + $(this).val());
}); 

Working demo

+29
source

You must check the condition when clicking or changing. I hope my example helps you.

    $("input:checkbox.country").click(function() {
        if(!$(this).is(":checked"))
        alert('you are unchecked ' + $(this).val());
    }); 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

    <input class="country" type="checkbox" name="country1" value="India" /> India </br>
    <input class="country" type="checkbox" name="country1" value="Russia" /> Russia <br>
    <input class="country" type="checkbox" name="country1" value="USA" /> USA <br>
    <input class="country" type="checkbox" name="country1" value="UK" /> UK
Hide result
+8
 $("#countries input:checkbox").on('change',function()
 {
   if(!$(this).is(':checked'))
      alert('uncheckd ' + $(this).val());
 }); 
+7

Source: https://habr.com/ru/post/1674950/


All Articles