Check if checked?

How to check if checkbox is checked via jQuery?

Is it possible to simply add an identifier or class to an element and do this?

if($('#element').val() == 1) { 
  //do stuff 
}
+4
source share
3 answers
if($('#element').is(':checked')){

    //checkbox is checked

}

or

if($('#element:checked').length > 0){

    //checkbox is checked

}

or in jQuery 1.6+:

if($('#element:checked').prop('checked') === true){

    //checkbox is checked

}
+2
source

It depends on where you are trying to do this. Typically, you can:

$('#element').is(':checked');

or

$('#element')[0].checked;

or

 $('#element').prop('checked'); 

or an older version of jquery (<1.6) that does not support prop, attr is used to perform the prop job, as well as to set / reset the properties of the element. (The inclusion of autonomous attributes, such as marked, selected, disabled, etc ...);

 $('#element').attr('checked') //will return boolean value

If this is in the context of the flag, for example, if in the event changeyou can simply do:

  this.checked
+4
source

- , jQuery, API :

$('input[type="checkbox"]').is(':checked') {
  // do the stuff here..
}

#element input[type="checkbox"]'.

This way you can find out that the checkbox is checked.

0
source

All Articles