How to check a radio button with jQuery?

There are two methods by which one can dynamically change the currently selected radio button by changing the checked property of the input type.

Method 1: Using 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 radio button
        with jQuery?
    </title>
      
    <script src=
    </script>
</head>
  
<body>
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
      
    <b>
        jQuery Check/Uncheck Radio Button
    </b>
      
    <p>
        <input type="radio" name="option" id="free">
            Free Membership
              
        <input type="radio" name="option" id="premium">
            Premium Membership
    </p>
      
    <p>
        <button type="button" class="check-free">
            Get Free
        </button>
          
        <button type="button" class="check-premium">
            Get Premium
        </button>
          
        <button type="button" class="reset">
            Reset options
        </button>
    </p>
      
    <script type="text/javascript">
        $(document).ready(function () {
            $(".check-free").click(function () {
                $("#free").prop("checked", true);
            });
            $(".check-premium").click(function () {
                $("#premium").prop("checked", true);
            });
            $(".reset").click(function () {
                $("#free").prop("checked", false);
                $("#premium").prop("checked", false);
            });
        });
    </script>
</body>
  
</html>                    

chevron_right


Output:

  • Clicking on the ‘Get Free’ button:
    free-checked
  • Clicking on the ‘Get Premium’ button:
    premium-checked
  • Clicking on the ‘Reset options’ button:
    reset-all

Method 2: Using 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 radio
        button with jQuery?
    </title>
      
    <script src=
    </script>
</head>
  
<body>
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
    <b>jQuery Check/Uncheck Radio Button</b>
    <p>
        <input type="radio" name="option" id="free">
            Free Membership
        <input type="radio" name="option" id="premium">
            Premium Membership
    </p>
      
    <p>
        <button type="button" class="check-free">
            Get Free
        </button>
          
        <button type="button" class="check-premium">
            Get Premium
        </button>
          
        <button type="button" class="reset">
            Reset options
        </button>
    </p>
      
    <script type="text/javascript">
        $(document).ready(function () {
            $(".check-free").click(function () {
                $("#free").attr("checked", true).click();
            });
            $(".check-premium").click(function () {
                $("#premium").attr("checked", true).click();
            });
            $(".reset").click(function () {
                $("#free").attr("checked", false);
                $("#premium").attr("checked", false);
            });
        });
    </script>
</body>
  
</html>                    

chevron_right


Output:

  • Clicking on the ‘Get Free’ button:
    free-checked
  • Clicking on the ‘Get Premium’ button:
    premium-checked
  • Clicking on the ‘Reset options’ button:
    reset-all


My Personal Notes arrow_drop_up

Technical Writer Lets have an update

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.