Skip to main content Accessibility Feedback


Get the next sibling of an element that matches a selector.

Requires the matches() polyfill.

 * Get next sibling of an element that matches selector
 * (c) 2018 Chris Ferdinandi, MIT License,
 * @param  {Node}   elem     The element
 * @param  {String} selector The selector to match against
 * @return {Node}            The sibling
var getNextSibling = function (elem, selector) {

	// Get the next sibling element
	var sibling = elem.nextElementSibling;

	// If there's no selector, return the first sibling
	if (!selector) return sibling;

	// If the sibling matches our selector, use it
	// If not, jump to the next sibling and continue the loop
	while (sibling) {
		if (sibling.matches(selector)) return sibling;
		sibling = sibling.nextElementSibling