/    Sign up×
Bounties /Pin to ProfileBookmark

How can you tell if a checkbox is checked using jQuery?

+ 1,000
Copy linkTweet thisAlerts:
Jan 05.2022
to post a answer
JavaScript

2 Replies

Copy linkTweet thisAlerts:
@JaySODec 05.2022 — To check if a checkbox is checked using jQuery, you can use the prop method to check the checked property of the checkbox. For example:


$('input[type="checkbox"]').prop('checked')


This code selects all the elements with the type attribute set to checkbox and then uses the prop method to check the checked property of the selected checkboxes. If the checked property is true, it means that the checkbox is checked, and if it is false, it means that the checkbox is not checked.

Alternatively, you can use the is method to check if the checkbox is checked. For example:


$('input[type="checkbox"]').is(':checked')


This code also selects all the elements with the type attribute set to checkbox, but instead of using the prop method, it uses the is method with the :checked selector to check if the selected checkboxes are checked. If the is method returns true, it means that the checkbox is checked, and if it returns false, it means that the checkbox is not checked.

In both cases, you can use the each method to iterate over the selected checkboxes and check if each one is checked. For example:


$('input[type="checkbox"]').each(function() {
if ($(this).prop('checked')) {
// Checkbox is checked
} else {
// Checkbox is not checked
}
});


This code uses the each method to iterate over the selected checkboxes and then uses the prop method to check the checked property of each checkbox. If the checked property is true, it means that the checkbox is checked, and if it is false, it means that the checkbox is not checked. You can add your own code inside the if statement to handle the checkbox being checked or not checked.
Copy linkTweet thisAlerts:
@7Feb 09.2023 — $(element).is(":checked")
https://api.jquery.com/is/
×

Success!

Help @hq spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...