In JavaScript è possibile reperire anche l'indice numerico di un dato elemento.
La soluzione è la seguente:
'use strict';
const index = (el, selector) => {
let i = 0;
while ((el = el.previousElementSibling) !== null) {
if (!selector || el.matches(selector)) {
++i;
}
}
return i;
}