I try to check a radio button with jQuery. Here's my code:
<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>
And the JavaScript:
jQuery("#radio_1").attr('checked', true);
Doesn't work:
jQuery("input[value='1']").attr('checked', true);
Doesn't work:
jQuery('input:radio[name="type"]').filter('[value="1"]').attr('checked', true);
Doesn't work:
Do you have another idea? What am I missing?
Try this.
To check Radio button using Value use this.
Or
Or
To check Radio button using ID use this.
Or
attr accepts two strings.
The correct way is:
One more function prop() that is added in jQuery 1.6, that serves the same purpose.
try this
if checked returns
True
if not checked returnsFalse
Just in case anyone is trying to achieve this while using jQuery UI, you will also need to refresh the UI checkbox object to reflect the updated value:
If property
name
does not work don't forget thatid
still exists. This answer is for people who wants to target theid
here how you do.Because property
name
does not work for me. Make sure you don't surroundid
andname
with double/single quotations.Cheers!