Accepted answer

A D3 selection is just a double-array wrapped around the element(s) selected. As you found with p3, you can dereference the arrays to find your first node, if you want. However, a better method does exist:

From the docs for selection.node():

Returns the first non-null element in the current selection. If the selection is empty, returns null.

In your case:

var dad = current_gene_pcp.node().parentNode;

However, if you don't need the line other than the DOM handle, you might as well just get that directly:

// Search document-wide...
var dad = document.querySelector(".line[name=" + current_gene_name + "]");

// ...or only as a child of the current DOM element
var dad = this.querySelector(".line[name=" + current_gene_name + "]");


For some reason, I had to go up 2x levels: console.log(">>"+my_chart.node()"<<")


One way I have done is:

bars.on('mouseover', d => {
let parentNode =;
let tooltip = parentNode.append('rect)

from this key word, you can access the current node, then touch its parentNode.


With the release of D3 v4 this has become an XY-problem of sorts. There is no longer the need to get to the parentNode to re-insert elements as the last child of its parent. Because from that release on D3 offers the selection.raise() method:


Re-inserts each selected element, in order, as the last child of its parent. Equivalent to:

selection.each(function() {

Using D3 v6 where the event is passed as the first parameter to the event listener (see changelog) your code can be as simple as this:

function onmouseover(event) {;

Have a look at the following demo:"g")
  .on("mouseover", event =>
<script src=""></script>

<svg width="400" height="400">
    <rect x="100" y="50" width="100" height="100" fill="red"></rect>
    <rect x="150" y="50" width="100" height="100" fill="blue"></rect>


There are two ways to access it.
Either use the third variable like this: someNode.attr("someAttrib", function(d, i, j) { console.log(d, i, j); });. The j contains the data you supplied to the parent node.
or use[0].id;.

An example:

<!DOCTYPE html>
<meta charset="utf-8">
<script src=""></script>
<div id="area", width="1000px" height="1000px"></div>
var GAP = 10, NODE_RADIUS = 14;

  var nodes = [
    {id: 1, prop: [{id: 1, something: 1}], abc: 1},
    {id: 2, prop: [{id: 1, something: 1}, {id: 2, something: 1}, {id: 3, something: 1}], abc: 2},
    {id: 3, prop: [{id: 1, something: 1}, {id: 2, something: 1}], abc: 3},
    {id: 4, prop: [{id: 1, something: 1}], abc: 4},
    {id: 5, prop: [{id: 1, something: 1}], abc: 5},
    {id: 6, prop: [], abc: 6}

var nodeLayer ="#area").selectAll(".node").data(nodes, function(d) {return; });
var iNodes = nodeLayer.enter().append("svg").attr("class", "node");
    //append the black circle
                .style("fill", "black")
                .attr("r", NODE_RADIUS)
                .attr("cx", function(d) {return 10 + * 10;})
                .attr("cy", 100);

    iNodes.append("g").selectAll(".prop").data(function(d) {return d.prop;}).enter()
                .text(function(d,i,j){console.log("parent id="+j); return[0].id;})
                .attr("dx", 50)
                .attr("dy", 50)
                .style("font-size", "8px")
                .style("fill", d3.rgb(180,0,0))
                .style("text-anchor", "middle");



Here's a quick way to move the mouseover element to the front:

selection.on("mouseover", function() { this.parentNode.appendChild(this); });

See also a related thread in the d3-js group.

Related Query

More Query from same tag