Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking if a checkbox is checked? [duplicate]

How can I check if a checkbox is checked via jQuery?

Can I just add an ID or class to the element and do this?

if($('#element').val() == 1) { 
  //do stuff 
}
like image 707
user2888263 Avatar asked Oct 19 '13 04:10

user2888263


1 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

}
like image 194
Ben Wong Avatar answered Sep 30 '22 02:09

Ben Wong