Blankman
Blankman

Reputation: 267320

how to figure out which radio button was selected?

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?

Upvotes: 1

Views: 233

Answers (3)

Mark Coleman
Mark Coleman

Reputation: 40863

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

Upvotes: 2

John K.
John K.

Reputation: 5474

...
<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>

Upvotes: 2

Trevor
Trevor

Reputation: 60270

This should return which radio buttons are currently checked:

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

More information here. Hope that helps!

Upvotes: 1

Related Questions