Skip to content

Instantly share code, notes, and snippets.

@cs09g
Last active September 13, 2018 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cs09g/dcbd4bd9a99803658e7473cb0aa9a818 to your computer and use it in GitHub Desktop.
Save cs09g/dcbd4bd9a99803658e7473cb0aa9a818 to your computer and use it in GitHub Desktop.
vanilla script next from jquery next
/*
* 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