Check and Uncheck a checkbox with jQuery

Topics related to client-side programming language.
Post questions and answers about JavaScript, Ajax, or jQuery codes and scripts.
MarPlo
Posts:186

Check and Uncheck a checkbox with jQuery

I want to do something like this:

Code: Select all

$(".chkox").checked(true);
Or:

Code: Select all

$(".chkox").selected(false);
I wish to set the "checked" value to check or uncheck the checkboxes with class="chkbox".
How can I do it with jQuery?

Admin Posts:805
Use the prop() method.

Code: Select all

$('.chkbox').prop('checked', true);  // checking
$('.chkox').prop('checked', false);  // unchecking
And if you want to check if a checkbox is checked or not, use this code:

Code: Select all

if($('#chk_id').is(':checked')) {
  // checkbox checked
}
else {
  // checkbox unchecked
}