check if checkbox is checked or not

If you want to learn how to check if checkbox is checked or not, read further for the details and the examples given below. There are two methods jQuery prop() and jQuery is() you can use to find the checkbox if it is checked or not.

But, before learning this, you should know how to make checkbox checked. So, let’s start learning.

How to Make Checkbox Checked on Button Click Using jQuery

You can use jQuery attr() to add the checked attribute to the checkbox.

Select your required HTML element using the jQuery selectors. In the attr(), you have to pass the property name as “checked” and the value as “true” or “false”. To make checkbox checked, you need to pass “true” as the value. To learn more about the attr() method visit jQuery attr() page .

Output


Name

The above example makes the checkbox checked on click of the button given. Click the button to see the changes.

How to Check If Checkbox is Checked or Not Using jQuery

To check if the checkbox is checked or not, use the prop() and is() methods. Below are the details and examples of each methods.

Check If Checkbox is checked Using prop()

The jQuery prop method provides you simple syntax to use in your code and check the status of the checkbox. The prop method works on the true and false condition of the checkbox is checked or not.

Output

Name

Click the checkboxes to check the status.

The above example gives you an alert message of whether the checkbox is checked or not. Click the checkbox given above to check the current status of the checkbox.

Check If Checkbox is checked Using is()

The is() method is the most used method to check the status of the checkbox or any other condition of HTML element. To find the checkbox is check, you need to pass “:checked” as the argument of the is(). If you want to check the checkbox is unchecked, pass “:not(:checked)” as an argument of the is().

Output

Name

Click the checkboxes to check the status.

The example given above gives you the same result as of the prop method. The syntax is the only difference between the prop() and is() methods.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.