How can I know which radio button is selected via jQuery?
To get the value of the selected radioName item of a form with id myForm: Here’s an example:
To get the value of the selected radioName item of a form with id myForm: Here’s an example:
Let’s pretend you have HTML like this For client-side validation, here’s some Javascript to check which one is selected: The above could be made more efficient depending on the exact nature of your markup but that should be enough to get you started. If you’re just looking to see if any radio button is selected anywhere on the page, PrototypeJS makes … Read more
TL;DR: Set the required attribute for at least one input of the radio group. Setting required for all inputs is more clear, but not necessary (unless dynamically generating radio-buttons). To group radio buttons they must all have the same name value. This allows only one to be selected at a time and applies required to … Read more
You can do something like this: jsfiddle Edit: Thanks HATCHA and jpsetung for your edit suggestions.
either (plain js) or (jQuery) See jQuery prop() help page for an explanation on the difference between attr() and prop() and why prop() is now preferable.prop() was introduced with jQuery 1.6 in May 2011.
Do not mix CSS/JQuery syntax (# for identifier) with native JS. Native JS solution: document.getElementById(“_1234”).checked = true; JQuery solution: $(“#_1234”).prop(“checked”, true);
The problem statement is simple. I need to see if user has selected a radio button from a radio group. Every radio button in the group share same id. The problem is that I don’t have control on how the form is generated. Here is the sample code of how a radio button control code … Read more
For versions of jQuery equal or above (>=) 1.6, use: For versions prior to (<) 1.6, use: Tip: You may also want to call click() or change() on the radio button afterwards. See comments for more info.
XHTML solution: Please note, that the actual value of checked attribute does not actually matter; it’s just a convention to assign “checked”. Most importantly, strings like “true” or “false” don’t have any special meaning. If you don’t aim for XHTML conformance, you can simplify the code to: