1

I have 2 radio buttons on my page:

<input id="rb1" type="radio" name="rmode" />
<input id="rb2" type="radio" name="rmode" />

How can I figure out which one was selected using jquery?

3 Answers 3

2
...
<input type="radio" name="sample" value="1" />
<input type="radio" name="sample" value="2" checked="checked" />
<input type="radio" name="sample" value="3" />
...

//javascript code

<script type="text/javascript">
<!--
    // displays the selected radio button in an alert box
    alert($('input[name=sample]:checked').val())
-->
</script>
2

You can use the :radio selector and also combine that with :checked to get the checked radio element.

This should work for you:

$("input[name=rmode]:radio:checked");

Example on jsfiddle

1

This should return which radio buttons are currently checked:

var id = $("input[@name=rmode]:checked").attr('id');

More information here. Hope that helps!

1
  • Sure, if you're using jQuery 1.2.6. It'll break in newer versions ;o)
    – user113716
    Commented Feb 1, 2011 at 17:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.