Skip to main content Accessibility Feedback

getNextUntil.js

Get all next siblings of an element until a test condition is met.

/*!
 * Get next siblings of an element until a test condition is met
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {Node}     elem     The element
 * @param  {Function} callback The test condition
 * @return {Array}             The siblings
 */
function getNextUntil (elem, callback) {

	// Setup siblings array and get next sibling
	let siblings = [];
	let next = elem.nextElementSibling;
	let index = 0;

	// Loop through all siblings
	while (next) {

		// If the matching item is found, quit
		if (callback && typeof callback === 'function' && callback(next, index, elem)) break;

		// Otherwise, push to array
		siblings.push(next);

		// Get the next sibling
		index++;
		next = next.nextElementSibling;

	}

	return siblings;

}