DEV Community

Jim Frenette
Jim Frenette

Posted on • Updated on • Originally published at jimfrenette.com

JavaScript Document Object (DOM) Helpers

Alt Text

A few DOM helpers to assist with the transition from jQuery to vanilla JavaScript.

indexInParent

export function indexInParent(el) {
    let children = el.parentNode.childNodes;
    let num = 0;

    for (let i = 0; i < children.length; i++) {
        if (children[i] == el) return num;
        if (children[i].nodeType == 1) num++;
    }
    return -1;
}

indexOfParent

export function indexOfParent(el) {
    return [].indexOf.call(el.parentElement.children, el);
}

matches

export function matches(elem, selector) {
    const isMsMatch = 'msMatchesSelector' in elem && elem.msMatchesSelector(selector);
    const isMatchSelector = 'matchesSelector' in elem && elem.matchesSelector(selector)
    const isMatch = 'matches' in elem && elem.matches(selector);
    // Test the element to see if it matches the provided selector
    // use different methods for compatibility
    return isMsMatch || isMatchSelector || isMatch;
    // Return the result of the test
    // If any of the above variables is true, the return value will be true
}

closest

For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.

Depends on matches;

export function getClosest(elem, selector) {
    // This allows for matching based on any selector, not just a single class.
    for (; elem && elem !== document; elem = elem.parentNode) {
        // Traverse up the dom until document is reached
        if (matches(elem, selector)) {
            // Test each element to see if it matches. If it does, return it.
            return elem
        }
    }
    return null;
}

export const closest = getClosest;

Usage for the above that's in a file set up for tree shaking, e.g., helpers.js

import { closest } from 'js/helpers';

offset top

export function getOffsetTop(el) {
    let offsetTop = 0;
    do {
        if (!isNaN(el.offsetTop)) {
            offsetTop += el.offsetTop;
        }
    } while (el = el.offsetParent);
    return offsetTop;
}

next

Get the immediately following sibling of each element in the set of matched elements.

Depends on matches, prev;

export function next(el, selector) {
    if (el.nextElementSibling) {
        if (matches(el.nextElementSibling, selector)) {
            return el.nextElementSibling;
        } else {
            return prev(el.nextElementSibling, selector);
        }
    }

    return false;
}

prev

Get the immediately preceding sibling of each element in the set of matched elements.

Depends on matches;

export function prev(el, selector) {
    if (el.previousElementSibling) {
        if (matches(el.previousElementSibling, selector)) {
            return el.previousElementSibling;
        } else {
            return prev(el.previousElementSibling, selector);
        }
    }

    return false;
}

siblings

Get the siblings of each element in the set of matched elements.

Depends on matches;

export function siblings(el, selector) {
    return Array.prototype.filter.call(el.parentNode.children, function (child) {
        return matches(child, selector);
    }) || [];
}

Originally published at jimfrenette.com/javascript/document

Top comments (1)

Collapse
 
ironydelerium profile image
ironydelerium

export function siblings(el, sel = '*') {
  let r = [...el.parentNode.querySelectorAll(`:scope > ${sel}`)];
  let p = r.indexOf(el);
  if (p != -1) r.splice(p, 1);
  return r;
}

Let the browser do the work. :scope in QSA refers to the node you're calling it on; siblings excludes el; and finally, a selector other than a simple selector won't match anything anyway because:

<ul>
  <li>first</li>
  <li class='second'>second</li>
</ul>

In jQuery, $('li.second').siblings('ul > li') would return an empty set - li.second doesn't have any ul siblings, nor are it's descendants siblings of it, so supporting anything but a simple selector is moot.