Created
March 10, 2016 07:46
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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