9

I'd like to know for a specific radio button group if a radio button is selected or not with jQuery.

Thanks

TheBoubou
  • 19,487
  • 54
  • 148
  • 236

4 Answers4

16
if( $('input[name=groupName]').is(':checked') ){
      //do something
}

or my original answer before Paulo woke me up

  if( $('input[name=groupName]:radio:checked').length ){
      //do something
  }
redsquare
  • 78,161
  • 20
  • 151
  • 159
3

I think what you are asking for "shouldn't be done" because the W3 states having no checked radio button in a group results in undefined behavior.

If no radio button in a set sharing the same control name is initially "on", user agent behavior for choosing which control is initially "on" is undefined.

Since user agent behavior differs, authors should ensure that in each set of radio buttons that one is initially "on".

Still, if you want to find the checked radio button, use:

var checkedRadioButtons = $(':radio:checked[name=XXX]');

Then check if one is checked:

if(!checkedRadioButtons.length) {
    alert('None checked!');
}
Community
  • 1
  • 1
strager
  • 88,763
  • 26
  • 134
  • 176
1
<script type="text/javascript">
($('input[name=groupName]').is(':checked')) ? 
    $('input[name=groupName]:checked', '#myForm').val() :
        null;
</script>
Milap
  • 6,915
  • 8
  • 26
  • 46
Swatantra Kumar
  • 1,324
  • 5
  • 24
  • 32
0

The other answers have covered finding if one radio out of a group is checked, however if you want to find if a particular button (not a button group) is checked:

$('#myRadioButton').attr('checked')

or

$('#myRadioButton').is(':checked')
nickf
  • 537,072
  • 198
  • 649
  • 721
  • 1
    `$('#myRadioButton').attr('checked')` won't work in all cases because it could be '', for example, which is empty but still has the button checked. – strager Aug 30 '09 at 07:20