-1

Does anyone know how to check a radio input with jQuery?

I was thinking val() would work but apparently it only sets the value on text inputs.

 $('[name="radio_group"]').val('my_selected_value');

Nothing happens if I do that

Magisch
  • 7,312
  • 9
  • 36
  • 52
xpedobearx
  • 707
  • 3
  • 8
  • 13

3 Answers3

1

Put the selected value into the selector, and set the checked property.

 $('[name="radio_group"][value="3"]').prop('checked', true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="radio_group" value="1">1<br/>
<input type="radio" name="radio_group" value="2">2<br/>
<input type="radio" name="radio_group" value="3">3<br/>
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

$('[name="radio_group"]').prop('checked',true);

This assumes that there is only one item with name ='radio_group'.

Pyro979
  • 202
  • 1
  • 10
  • 1
    Making assumptions about code you haven't been shown isn't a great thing to do, especially when you consider that the main function of radio inputs is achieved by multiple inputs having the same name. – Ken Herbert Aug 04 '15 at 01:31
  • @winterblood fair enough. – Pyro979 Aug 04 '15 at 01:56
0

try

jQuery("#radio_1").attr('checked', true);
jameshwart lopez
  • 2,993
  • 6
  • 35
  • 65