0

How to know a div's nth child by pure js?

<div>One</div>
<div>Two</div>
<div id="myDiv">Three</div>
<div>Four</div>

<script>
var elm=document.getElementById("myDiv");
alert(elm.getChildNumber());
</script>

This code doesn't works.

Now how will I know the nth-child number of #myDiv ?

Thanks in advance!!

Devs
  • 29
  • 4
  • What have you tried so far to solve this on your own? - [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – Andreas Nov 26 '21 at 10:46
  • I used elm.getChildNumber(); – Devs Nov 26 '21 at 10:47
  • There's no such thing as `.getChildNumber()` o.O -> [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Andreas Nov 26 '21 at 10:48
  • Get the parent element, iterate over the `.children` and compare (`===`) with the element you're looking for. – Andreas Nov 26 '21 at 10:49

0 Answers0