Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get value of checked radio button using d3.js

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
like image 674
Brett Avatar asked Mar 29 '15 02:03

Brett


People also ask

What is the value of a checked radio button?

The checked property returns True if the radio button is selected and False otherwise.

How can I get radio button checked?

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> .

How do you find the value of a radio button group?

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.


2 Answers

Try this

d3.select('input[name="group-stack"]:checked').node().value
like image 73
geekychick Avatar answered Sep 27 '22 19:09

geekychick


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");
like image 33
jorgeh Avatar answered Sep 27 '22 19:09

jorgeh