Check if checkbox is checked

Posted by nikola, With 0 Comments, Category: JavaScript && jQuery, Tags:

If you want to check if your checkbox with ID defined as option_2 is checked, you can use next code:

if ( $('#option_2').attr('checked') ) {
 alert('Yep, your checkbox is checkd');
 } else {
 alert('Nope, your checkbox is not checked');

Also, if you want to set some of your checkboxes as checked, you can do:

$("#option_2").attr("checked", true);

On the other side, you will uncheck it with:

$("#option_2").attr("checked", false);