I’m having some strange problem with my JS program. I had this working properly but for some reason it’s no longer working. I just want to find the value of the radio button
This is pure JavaScript, based on the answer by @Fontas but with safety code to return an empty string (and avoid a TypeError
) if there isn't a selected radio button:
var genderSRadio = document.querySelector("input[name=genderS]:checked");
var genderSValue = genderSRadio ? genderSRadio.value : "";
The code breaks down like this:
type, (b) has a name
attribute of genderS
, and (c) is checked.genderSRadio
variable is truthy if Line 1 finds the control and null/falsey if it doesn't.For JQuery, use @jbabey's answer, and note that if there isn't a selected radio button it will return undefined
.