How to check a checkbox with jQuery?

There are two methods by which you can dynamically check the currently selected checkbox by changing the checked property of the input type.

Method 1: Using the prop method: The input can be accessed and its property can be set by using the prop method. This method manipulates the ‘checked’ property and sets it to true or false depending on whether we want to check or uncheck it.
Syntax:

$("element").prop("checked", true)

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
  
<head>
    <title>
        How to check a checkbox with jQuery?
    </title>
  
    <script src=
    </script>
</head>
  
<body>
    <center>
        <h1 style="color: green"
          GeeksforGeeks 
        </h1>
  
        <b
          jQuery Check/Uncheck Checkbox
        </b>
  
        <p>
            <input type="checkbox" name="option" id="front"
            Front-End
  
            <input type="checkbox" name="option" id="back">
            Back-End
        </p>
  
        <p>
            <button type="button" class="check-front">
                Subscribe Front-End
            </button>
  
            <button type="button" class="check-back">
                Subscribe Back-End
            </button>
  
            <button type="button" class="reset">
                Reset 
            </button>
        </p>
  
        <script type="text/javascript">
            $(document).ready(function() {
                $(".check-front").click(function() {
                    $("#front").prop("checked", true);
                });
                $(".check-back").click(function() {
                    $("#back").prop("checked", true);
                });
                $(".reset").click(function() {
                    $("#front").prop("checked", false);
                    $("#back").prop("checked", false);
                });
  
            });
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:

  • Before clicking any button:
  • Clicking on the button:
  • Clicking on the ‘Reset’ button:

Method 2: Using the attr method: It is similar to the above method and more suitable for older jQuery versions. The input can be accessed and its property can be set by using the attr method. We have to manipulate the ‘checked’ property and set it to true or false depending on whether we want to check or uncheck it.
Note: It is necessary to add a click method when setting the attribute to ‘true’ to make sure that the option gets updated in the option group.
Syntax:

$("element").attr("checked", true)

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
  
<head>
    <title>
        How to check a checkbox with jQuery?
    </title>
  
    <script src=
    </script>
</head>
  
<body>
    <center>
        <h1 style="color: green"
        GeeksforGeeks 
    </h1>
  
        <b
        jQuery Check/Uncheck Checkbox
    </b>
  
        <p>
            <input type="checkbox" name="option" id="Front"
            Front-End
  
            <input type="checkbox" name="option" id="Back"
            Back-End
        </p>
  
        <p>
            <button type="button" class="check-Front">
                Subscribe Front-End
            </button>
  
            <button type="button" class="check-Back">
                Subscribe Back-End
            </button>
  
            <button type="button" class="reset">
                Reset 
            </button>
        </p>
        <script type="text/javascript">
            $(document).ready(function() {
                $(".check-Front").click(function() {
                    $("#Front").attr("checked", true);
  
                });
                $(".check-Back").click(function() {
                    $("#Back").attr("checked", true);
                });
                $(".reset").click(function() {
                    $("#Front").attr("checked", false);
                    $("#Back").attr("checked", false);
                });
            });
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:

  • Before clicking any button:
  • Clicking on the button:
  • Clicking on the ‘Reset’ button:


My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.