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
Linda HamiltonOriginal
2024-12-25 14:07:12589browse

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);

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

$('.myCheckBox').attr('checked', true);

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

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

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!

Statement:
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