I have the following form:
<form>
<input type="radio" name="group-stack" value="grouped" checked>grouped<br>
<input type="radio" name="group-stack" value="stacked">stacked
</form>
I want to get the currently selected radio button's value using d3.
The following attempts have been unsuccessful:
var val = d3.select('input[name="group-stack"]').checked; //undefined
var val = d3.select('input[name="group-stack"][checked]')[0][0].value //always 'grouped' regardless of which radio is selected
The checked property returns True if the radio button is selected and False otherwise.
You can check a radio button by default by adding the checked HTML attribute to the <input> element. You can disable a radio button by adding the disabled HTML attribute to both the <label> and the <input> .
Answer: Use the jQuery :checked selector You can simply use the jQuery :checked selector in combination with the val() method to find the value of the selected radio button inside a group.
Try this
d3.select('input[name="group-stack"]:checked').node().value
I'm a little late to the party but, FWIW, this is the pure d3 way:
d3.select('input[name="group-stack"]:checked').property("value");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With