1

I know how to select the nth element of a certain type document.querySelectorAll('.myClass')[n],

but given an element myEl = document.querySelector('.myClass .myOtherClass'),

how do I then determine its place (its "n") in the array of elements selected by document.querySelectorAll('.myClass')?

ultraGentle
  • 5,084
  • 1
  • 19
  • 45

2 Answers2

1

do this:

document.querySelectorAll(".myClass").indexOf(yourElement) // ==> your index

lolBOT V9.17
  • 141
  • 10
1

U can try something like that:

Array.prototype.indexOf.call(document.querySelectorAll('.myclass'),myEl)