78

I'm trying to get the value of two radio button groups using the jQuery syntax as given below. When the code below is run I get the value selected from the first radio button group twice instead of getting the value of each individual group.

Am I doing something obviously wrong here? Thanks for any help :)

<a href='#' id='check_var'>Check values</a><br/><br/>
<script>
  $('a#check_var').click(function() {
    alert($("input:radio['name=r']:checked").val()+ ' ' +
          $("input:radio['name=s']:checked").val());
  });
</script>
Group 1<br/>
<input type="radio"  name="r" value="radio1"/> radio1
<input type="radio"  name="r" value="radio2"/> radio2
<br/><br/>
Group 2<br/>
<input type="radio"  name="s" value="radio3"/> radio3
<input type="radio"  name="s" value="radio4"/> radio4
Dhanu Gurung
  • 8,480
  • 10
  • 47
  • 60
Parampal Pooni
  • 2,958
  • 8
  • 34
  • 40
  • possible duplicate of [How to get the value of a selected radio button using its name in jQuery?](http://stackoverflow.com/questions/986120/how-to-get-the-value-of-a-selected-radio-button-using-its-name-in-jquery) – jeremy Jul 04 '14 at 03:34

1 Answers1

153

Your quotes only need to surround the value part of the attribute-equals selector, [attr='val'], like this:

$('a#check_var').click(function() {
  alert($("input:radio[name='r']:checked").val()+ ' '+
        $("input:radio[name='s']:checked").val());
});​

You can see the working version here.

Boric
  • 822
  • 7
  • 26
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155