Get the index position of an HTMLElement in a container with D3

Posted on February 5, 2021

If you want to know the index position of an SVG element in a collection, such as the SVG itself or a group, here the solution:

elements.on('click', function(e, d){
  
    const list = [...this.parentNode.children]
    const index = list.indexOf(this)
    console.log(index)
  
})

Here a live example.

Want to interact? Reach me out on Twitter
Spotted a typo? Send a patch