Home > Web Front-end > JS Tutorial > How Do I Programmatically Check a Checkbox with jQuery?

How Do I Programmatically Check a Checkbox with jQuery?

Linda Hamilton
Release: 2024-12-25 14:07:12
Original
591 people have browsed it

How Do I Programmatically Check a Checkbox with jQuery?

Setting "checked" for a checkbox with jQuery

You can set the "checked" attribute of a checkbox using the .prop() method in jQuery:

$('.myCheckBox').prop('checked', true);
Copy after login

If you're working with jQuery versions prior to 1.6, you can use the .attr() method instead:

$('.myCheckBox').attr('checked', true);
Copy after login

You can also use the underlying HTMLInputElement to modify the .checked property:

$('.myCheckBox')[0].checked = true;
Copy after login

One should use .prop() and .attr() methods instead of this as they will operate on all matched elements.

The above is the detailed content of How Do I Programmatically Check a Checkbox with jQuery?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template