Skip to content

Instantly share code, notes, and snippets.

@droid001
Created March 10, 2016 07:46
Show Gist options
  • Save droid001/b4d2d5ec2b2f081a15a9 to your computer and use it in GitHub Desktop.
Save droid001/b4d2d5ec2b2f081a15a9 to your computer and use it in GitHub Desktop.
Returns the index of a given element in relation to its siblings, optionally restricting siblings to those matching a provided selector.
/**
* @param {Element} el
* @param {string} [selector]
* @return {number}
*/
h.index = function(el, selector) {
var i = 0;
while ((el = el.previousElementSibling) !== null) {
if (!selector || el.matches(selector)) {
++i;
}
}
return i;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment