Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. DarrenSem revised this gist Apr 14, 2025. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions above(parentElement).js + next(siblingElement).js
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,16 @@
    // above.js (PARENT element, levels = #) + next.js (SIBLING element, levels = #) helper functions instead of foo.parentElement.parentElement.parentElement etc.

    // https://gist.github.com/DarrenSem/4d37711fb71a484b6ed2356c60e3c94a


    // PARENT element: ...
    const above = (startingElement, levels = 1) => {
    let el = startingElement;
    while ( levels-- && (el = el?.parentElement) );
    return el || null;
    };


    // ... cf. SIBLING element:
    const next = (startingElement, count = 1) => {
    let el = startingElement;
  2. DarrenSem created this gist Apr 14, 2025.
    15 changes: 15 additions & 0 deletions above(parentElement).js + next(siblingElement).js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    // above.js (PARENT element, levels = #) + next.js (SIBLING element, levels = #) helper functions instead of foo.parentElement.parentElement.parentElement etc.

    // PARENT element: ...
    const above = (startingElement, levels = 1) => {
    let el = startingElement;
    while ( levels-- && (el = el?.parentElement) );
    return el || null;
    };

    // ... cf. SIBLING element:
    const next = (startingElement, count = 1) => {
    let el = startingElement;
    while ( count-- && (el = el?.nextElementSibling) );
    return el || null;
    };