views:

15561

answers:

6

I have two radio buttons and want to post the value of the selected one, how can I get the value with jQuery?

I can get all of them like this:

$("form :radio")

But how do I know which one is selected?

A: 

The val() attribute will tell you. So $("form :radio").val()

More info at the jQuery docs.

bbrown
hmm... I'm getting always the same value no matter which one is selected
Juan Manuel
Are you sure you have selected the correct radio button? The form:radio will select all radio buttons on the page.
acrosman
`val()` does not account for radio button groups. You need `:checked` for that.
Crescent Fresh
Sorry, the documentation wasn't very clear about radio buttons. I'd used it before with dropdowns and I figured that it would return the selected value.
bbrown
+39  A: 

To get the value of the selected radioName item of a form called 'myForm':

$('input[name='radioName']:checked', '#myForm').val()
Peter J
I ended up using this: $('form input[type=radio]:checked')
Juan Manuel
Glad you got it working!
Peter J
This was helpful...
CarolinaJay65
+3  A: 

You can use the :checked selector along with the radio selector.

 $("form :radio").find(":checked").val();
tvanfosson
+3  A: 

Use this..

$("#myform input[type='radio']:checked").val();
joberror
A: 

In a JSF generated radio button (using <h:selectOneRadio> tag), you can do this:

radiobuttonvalue = jQuery("input[name='form_id\:radiobutton_id']:checked").val();

where selectOneRadio ID is radiobutton_id and form ID is form_id.

Be sure to use name instead id, as indicated, because jQuery uses this attribute (name is generated automatically by JSF resembling control ID).

nihcap
A: 

$("input:radio:checked").val();

cf_PhillipSenn