XmanyX
XmanyX

Reputation: 37

Retrieve node name by it's ID. (D3JS)

I am currently trying to have a sort of "general" tooltip. In here, the name and some other data from the currently "active" node are shown. E.G. when a node with id 3 is the active node, the tooltip shows the name and, for example, distance (not link distance!) of the node with id=3.

However, I cant seem to select the given node. I keep getting "SyntaxError: Document.querySelector: '#3' is not a valid selector"

Below is the code:

var currNode = 3; // The active node ID
var position = d3.select("#" + currNode).name // The data I want to show, e.g. the name

And then the variable "position" is shown in the tooltip using

var tooltip = this.svg.append("text")
      .attr("x", 25)
      .attr("y", 25)
      .text("Your current position: " + position )
      .style("font-weight", 550)
      .style("font-size", 18)

Does anyone have a (different) way of accessing the data of a given node?

Thanks in advance!

Upvotes: 2

Views: 659

Answers (1)

Michael Rovinsky
Michael Rovinsky

Reputation: 7210

To find an object by id in array of objects:

const nodeName = nodes.find(node => node.id === id).name;

Upvotes: 1

Related Questions