Home >Web Front-end >JS Tutorial >How Do I Check or Uncheck a Checkbox Using jQuery?
To select a checkbox using jQuery, we can use the .prop() method to modify the underlying HTMLInputElement's .checked property.
Use .prop():
$('.myCheckbox').prop('checked', true); $('.myCheckbox').prop('checked', false);
If working with a single element:
$('.myCheckbox')[0].checked = true; $('.myCheckbox')[0].checked = false;
Before jQuery version 1.6, use .attr() instead of .prop():
$('.myCheckbox').attr('checked', true); $('.myCheckbox').attr('checked', false);
Note that this approach is preferred over using .removeAttr('checked'), as it prevents unintended behavior changes when calling .reset() on forms containing the checkbox.
The above is the detailed content of How Do I Check or Uncheck a Checkbox Using jQuery?. For more information, please follow other related articles on the PHP Chinese website!