How to select element in d3.js
This is the very first step learning d3.js, we must know how to select the element.
It's very similar to jQuery, just using console in your browser to check it out.
Demo example
Another demo example
-
Try
d3.select("h3")
to get the first h3
, assign it to h3
.
-
Like
addClass("className")
in jQuery, we have h3.class("my-class")
-
You may notice it would replace all the class, so you can add specific class by using
h3.classed("another-class", true)
, remove by h3.classed("another-class", false)
-
And you can use
h3.attr("attribute", "value")
to add attribute and assign value to it.
-
Using
h3.style("color", red)
to change color.
-
And we can use
h3.selectAll("h3")
to select multiple elements.
-
Just play around in console.
Back to menu