Open In App

How to Check whether a Checkbox is Checked in jQuery?

Last Updated : 20 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

We can use prop() and is() methods to check whether a checkbox is checked in jQuery. A checkbox is a user interface element that allows users to select or deselect an option.

1. Using prop() Method

This prop() method provides a simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.

Syntax

$(selector).prop(parameter) 
html
<!DOCTYPE html>
<html lang="en">

<head>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
</head>

<body>
    <input type="checkbox" name="radio1" />

    <button style="margin-top:10px" id="but" type="submit">
        Submit
    </button>

    <script>
        $(document).ready(function () {
            $("#but").click(function () {
                if ($("input[type=checkbox]").prop("checked")) {
                    alert("Check box is Checked");
                } else {
                    alert("Check box is Unchecked");
                }
            });
        });
    </script>
</body>

</html>

Output

2. Using jQuery is() Method

In this approach we are using the is() method of jQuery. That methods check if the given element consist of the given situation and returns the output according to it. This method is also very simple and easy to use. By using this we can easily find whether a checked box is checked or not.

Syntax

$(selector).is(parameter) 
html
<!DOCTYPE html>
<html lang="en">

<head>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
</head>

<body>
    <input type="checkbox" name="radio1" />
    <button id="but" type="submit">
        Submit
    </button>

    <script>
        $(document).ready(function () {
            $("#but").click(function () {
                if ($("input[type=checkbox]").is(
                    ":checked")) {
                    alert("Check box in Checked");
                } else {
                    alert("Check box is Unchecked");
                }
            });
        });
    </script>
</body>

</html>

Output



Next Article

Similar Reads