kreativan
5/14/2019 - 2:30 PM

JavaScript Get Element Siblings

/**
 *  Get Element Siblings
 *  next or prev elements
 */
function getSiblings(element, type = 'next'){
    var arraySib = [];
    if (type == 'prev'){
        while ( element = element.previousSibling ){
            arraySib.push(element);
        }
    } else if (type == 'next') {
        while ( element = element.nextSibling ){
            arraySib.push(element);
        }
    }
    return arraySib;
}