Accessing d3.js element attributes from the datum?

Your code is trying to get an svg attribute from an item of data, when what you really want is to get that attribute from the svg DOM element, as in:


This will only give you the attribute for the first non-null element of your selection; You can also filter your selection to get the DOM element you are looking for:


Or, if you’d like to access the attributes of all selected elements, use this in your each function:

d3.selectAll(".mynode").each( function(d, i){
  if(d.someId == targetId){
    console.log("cx") );

Leave a Comment