Last active
September 13, 2018 01:27
-
-
Save cs09g/dcbd4bd9a99803658e7473cb0aa9a818 to your computer and use it in GitHub Desktop.
vanilla script next from jquery next
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
/* | |
* It returns next element sibling | |
* works like jquery's `next` but combined selector is not supported | |
* | |
*/ | |
var next = function(target, selector) { | |
var siblings = target.parentNode.children; | |
var nextElementSibling; | |
for (var i = Array.prototype.indexOf.call(siblings, target) + 1; i < siblings.length; i++) { | |
if (selector[0] === ".") { | |
if (siblings[i].classList.contains(selector.substr(1))) { | |
nextElementSibling = siblings[i]; | |
break; | |
} | |
} else if (selector[0] === "#") { | |
if (siblings[i].id === selector.substr(1)) { | |
nextElementSibling = siblings[i]; | |
break; | |
} | |
} else { | |
if (siblings[i].tagName.toLowerCase() === selector) { | |
nextElementSibling = siblings[i]; | |
break; | |
} | |
} | |
} | |
return nextElementSibling; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment