Skip to content

Instantly share code, notes, and snippets.

@kcsluis
Created September 12, 2015 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kcsluis/0b1c715c5709ed6d5602 to your computer and use it in GitHub Desktop.
Save kcsluis/0b1c715c5709ed6d5602 to your computer and use it in GitHub Desktop.
Buttons, Dropdowns
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
/*css*/
</style>
</head>
<body>
<!-- html -->
<select></select>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script>
<script>
// key interaction
window.onkeyup = function(e) {
var key = e.keyCode ? e.keyCode : e.which;
if (key == 39) {
console.log('Right');
} else if (key == 37) {
console.log('Left');
} else if (key == 38) {
console.log('Up');
} else if (key == 40) {
console.log('Down');
};
};
// buttons w data join
var buttons = d3.select("body").selectAll(".controller-button")
.data(["I", "II", "III", "IV"])
.enter()
.append("button")
.attr("class", "controller-button")
.text(function(d) { return "Group " + d; })
.on("click", function(d) {
console.log('Button pressed')
});
// dropdown menu
d3.select("select").selectAll("option")
.data(['A','B','C','D'])
.enter()
.append("option")
.text( function (d) { return d; });
// the event listener has to be associated with the select, not the option
d3.select('select')
.on('change', function (d) { console.log(this.value); });
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment