How to check the radio button in jQuery

Asked 2 years ago, Updated 2 years ago, 49 views

I'd like to check the radio button with jQuery

<form>
    <div id='type'>
        <input type='radio' id='radio_1' name='type' value='1' />
        <input type='radio' id='radio_2' name='type' value='2' />
        <input type='radio' id='radio_3' name='type' value='3' /> 
    </div>
</form>

This is my code here

jQuery("#radio_1").attr('checked', true);

jQuery("input[value='1']").attr('checked', true);

jQuery('input:radio[name="type"]').filter('[value="1"]').attr('checked', true);

I tried these methods, but they didn't work. What should I do?

javascript jquery

2022-09-22 14:58

1 Answers

$("#radio_1").prop("checked", true)

$("#radio_1").attr('checked', 'checked');

You can do it like above. You can use the bottom one when jQuery is less than 1.6 version.


2022-09-22 14:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.