Skip to main content Accessibility Feedback

getPreviousSiblings.js

Get the previous sibling of an element that matches a test condition.

/*!
 * Get previous sibling of an element that matches a test condition
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {Node}     elem     The element
 * @param  {Function} callback The test condition
 * @return {Node}              The sibling
 */
function getPreviousSibling (elem, callback) {

	// Get the next sibling element
	let sibling = elem.previousElementSibling;

	// If there's no callback, return the first sibling
	if (!callback || typeof callback !== 'function') return sibling;

	// If the sibling matches our test condition, use it
	// If not, jump to the next sibling and continue the loop
	let index = 0;
	while (sibling) {
		if (callback(sibling, index, elem)) return sibling;
		index++;
		sibling = sibling.previousElementSibling;
	}

}