getNextSibling.js
Get the next sibling of an element that matches a test condition.
/*!
* Get next 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 getNextSibling (elem, callback) {
// Get the next sibling element
let sibling = elem.nextElementSibling;
// 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.nextElementSibling;
}
}