Created
April 14, 2025 15:48
Revisions
-
DarrenSem revised this gist
Apr 14, 2025 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; -
DarrenSem created this gist
Apr 14, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; };