Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking a radio button in JQuery

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.

like image 988
bba Avatar asked Oct 12 '10 12:10

bba


2 Answers

So you want to select the radio which has a particular value:

$('input[name=my_name][value=123]').attr('checked', true); // or 'checked'
like image 79
nickf Avatar answered Oct 05 '22 23:10

nickf


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);
like image 20
anand Avatar answered Oct 05 '22 23:10

anand