Skip to content

Instantly share code, notes, and snippets.

@jfreels
Last active May 27, 2021 09:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfreels/6734823 to your computer and use it in GitHub Desktop.
Save jfreels/6734823 to your computer and use it in GitHub Desktop.
d3js: Create an HTML select box using d3.js and create a paragraph referencing the selection.

d3js: Create an HTML select box using d3.js and create a paragraph referencing the selection.

<!DOCTYPE html>
<meta charset='utf-8'>
<html>
<head>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<link rel='stylesheet' href='style.css'>
</head>
<body>
<script type='text/javascript' src='script.js'></script>
</body>
</html>
var data = ["Option 1", "Option 2", "Option 3"];
var select = d3.select('body')
.append('select')
.attr('class','select')
.on('change',onchange)
var options = select
.selectAll('option')
.data(data).enter()
.append('option')
.text(function (d) { return d; });
function onchange() {
selectValue = d3.select('select').property('value')
d3.select('body')
.append('p')
.text(selectValue + ' is the last selected option.')
};
/* CSS Here
@adamwlev
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment