d3.select by attribute value

d3.select by attribute value

I am new to d3. I have something defined like this:
node = node.enter().append(“circle”)
.attr(‘id’, function(d){ return d.id; })
.attr(“class”, “node”)
.on(‘mouseover’, mouseover_node)
.on(“click”, nodeClick);

Now in function nodeClick I want to access a node (or circle) with a special id. I am looking for something that I could use like this:
for(var i=0;i

Solutions/Answers:

Solution 1:

Your problem is that ids and names must begin with a letter. So modify your code to prepend a string to each id, e.g.

.attr('id', function(d){ return 'name' + d.id; })

Then, you can select a given node by using d3.select( '#name' + i ). From the docs on D3 selections:

… you can select by tag (“div”), class (“.awesome”), unique
identifier (“#foo”), attribute (“[color=red]”), or containment
(“parent child”).

References

Related:  How to catch and handle error response 422 with Redux/Axios?