The filter method in the accepted answer is correct. The second approach in the accepted answer (using .each) is incorrect and contains the same error as the questioner: if .data () is not called (which is the case here), then the first argument d passed to .each will be undefined (and not " current dom node ", as all newbies would expect, including me); you get the current dom node through d3.select (this), which is true in the if statement itself at the very end - the error is in the if condition. The following is the correct version.
d3.selectAll(".mynode").each(function(d,i){ var elt = d3.select(this); if (elt.attr("id") == "yourTargetIdGoesHere"){ console.log( elt.attr("cx") ); } });
script: fiddle (containing code for both versions, i.e. filter and each)
UPDATE: my answer suggested that you did not use .data () since you did not give the code for this; I later saw that you wrote that you used .data ()
in this case, depending on your data structure, replacing d.attr ("cx") with plain d.cx might work.
mathheadinclouds May 21 '14 at 16:25 2014-05-21 16:25
source share