How to Check a Radio Button in a Group with jquery

To check or select a Radio form control we use prop() method in jQuery. For that, we need to bind the prop method using a selector which can be an ID, class or value of radio control in a group.

As shown below, there is a group of Radio controls with the same name myRadio

    <p>
        <h5>Radio Selection</h5>
        
        <label>
            <input type="radio" name="myRadio" value="1"> Option 1
        </label>
        <label>
            <input type="radio" name="myRadio" value="2"> Option 2
        </label>
        <label>
            <input type="radio" name="myRadio" value="3"> Option 3
        </label>
    </p>

    <p>
        <button>Check Radio Option 2</button>
    </p>

Now if we want to check second radio with value 2 we can do so using prop() method in the following way

    <script>
        $(function () {

            $("button").click(function () {
                $("input:radio[value='2']").prop('checked',true);
            });

        });
    </script>

 

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments