How can we write an unchecked event for a checkbox in jquery ?. How do we do for a verification event?

I tried below with a marked event

$(document).ready(function(){
  $('.same').change(function(){
    if(this.checked){
      alert("checked");
    }
  });
});
Run codeHide result

I did this for a marked event, how can I do the same for an uncontrolled event.

+4
source share
3 answers

If you want only an event unchecked, then do !this.checkedas shown below: -

$(document).ready(function(){
  $('.same').change(function(){
    if(!this.checked){
      alert("unchecked");
    }
  });
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="checkbox" class="same">Check/Uncheck me to see the alerts!
Run codeHide result

If you want both events checked/unchecked, then you can use there else: -

Example: -

$(document).ready(function(){
  $('.same').change(function(){
    if(this.checked){
      alert("checked");
    }else{
      alert("unchecked");
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="checkbox" class="same">Check/Uncheck me to see the alerts!
Run codeHide result
+1
source

Try the following:

if( !this.checked ){

! notused here with checked, this means that it refers to unchecked. Or you can use elseas suggested by @Alive.

+1
$(document).ready(function(){
    $('.same').change(function(){
                if(!this.checked){
                       alert("not checked");
                 }
       });
 });

! this.checked

+1

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


All Articles