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?
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?
The val()
attribute will tell you. So $("form :radio").val()
More info at the jQuery docs.
To get the value of the selected radioName item of a form called 'myForm':
$('input[name='radioName']:checked', '#myForm').val()
You can use the :checked selector along with the radio selector.
$("form :radio").find(":checked").val();
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).