I need to programmatically check a radio button given its value. The form has an id
and the input type obviously has a name (but no id). The only code I managed to get working so far is:
$('input[name=my_name]:eq(1)').attr('checked', 'checked');
But I'd like to be able to check it by explicitly providing the value.
So you want to select the radio which has a particular value:
$('input[name=my_name][value=123]').attr('checked', true); // or 'checked'
Below code worked with me, if I am assigning an ID to the radio button:
<input type="radio" id="rd_male" name="gender" value="Male" />
<input type="radio" id="rd_female" name="gender" value="Female" />
$('#rd_male').prop('checked', true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With