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?
From this question, I came up with an alternate way to access the currently selected
input
when you're within aclick
event for its respective label. The reason why is because the newly selectedinput
isn't updated until after itslabel
's click event.TL;DR
Here's how I would write the form and handle the getting of the checked radio.
Using a form called myForm:
Get the value from the form:
If you're not posting the form, I would simplify it further by using:
Then getting the checked value becomes:
Having a class name on the input allows me to easily style the inputs...
DEMO : https://jsfiddle.net/ipsjolly/xygr065w/
To retrieve all radio buttons values in JavaScript array use following jQuery code :
Another option is: