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")
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")
How do I know which one is selected?
In a JSF generated radio button (using
<h:selectOneRadio>
tag), you can do this: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).
Use this:
You need access with the
:checked
selector:Check this doc:
a example:
If you already have a reference to a radio button group, for example:
Use the
filter()
function, notfind()
. (find()
is for locating child/descendant elements, whereasfilter()
searches top-level elements in your selection.)Notes: This answer was originally correcting another answer that recommended using
find()
, which seems to have since been changed.find()
could still be useful for the situation where you already had a reference to a container element, but not to the radio buttons, e.g.: