Search code examples
d3.jsselectionnodesparent

Easiest way to select parent nodes multiple parents up in the tree in D3


I'm trying to select the parent four levels up in the tree from my current selection.

Short of doing

d3.select(d3.select(this.parentNode)[0][0].parentNode)

up to four levels, is there an easier way to access the parent of a parent of a parent, or such similar?


Solution

  • You could skip the nested selections and do d3.select(this.parentNode.parentNode.parentNode...etc), but that's about it - .parentNode is the only way to go and according to the author it's by design: https://groups.google.com/forum/#!topic/d3-js/mEjem7IPshY