-2

I have an element and that element is within a parent div. I want to see what the child id is of this element, preferably without looping. Any suggestions?

anonymous-dev
  • 2,897
  • 9
  • 48
  • 112
  • Does this answer your question? [JavaScript DOM: Find Element Index In Container](https://stackoverflow.com/questions/11761881/javascript-dom-find-element-index-in-container) – 04FS Nov 19 '19 at 09:30

1 Answers1

1

You can convert the children set of its parent to an array and look for the index:

const element = document.querySelector('xxx');

const index = [...element.parentElement.children].indexOf(element);
Hao Wu
  • 17,573
  • 6
  • 28
  • 60
  • Best answer, no loop needed. Love it. I did get confused by the 3 dots tho. What do they do? – anonymous-dev Nov 19 '19 at 11:02
  • @MikeOttink The three dots here is for object destruction, then wrap it in squarely brackets to make a array out of them. See https://stackoverflow.com/questions/31048953/what-do-these-three-dots-in-react-do for more information – Hao Wu Nov 19 '19 at 11:11