Scenario: I have several svg
elements on a page, each displaying a different graph. When a graph receives a mouse click it triggers an event handler wherein this
corresponds to the raw svg
element that was clicked. When this happens, I want to select the graph's path
element with D3 in order modify it.
I know that I could give each graph an ID and then use that to make a D3 selection, e.g.
function on_click( event ) {
var path = d3.select( '#' + this.id ).select( 'path' );
path.do_stuff...
}
but I wondered whether there was an equivalent of jQuery's feature of turning raw DOM elements in to a jQuery object, e.g.
jQuery( my_raw_dom_element ).do_stuff...
Select Element by IdThe d3. select() method can also be used to get an element with specified id as shown below. As you can see in the above example, d3. select("#p2") selects <p> element whose id is p2 and makes it green using .
SVG is text-based, and it is an image format that is vector-based. SVG is the same as the HTML structure. It can be illustrated as the DOM (Document Object Model). The properties of SVG can be described as attributes.
d3. select selects the first matching element whilst d3. selectAll selects all matching elements. Both functions take a string as its only argument.
Yes, you can simply do
d3.select(my_raw_dom_element);
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