Created
April 14, 2025 15:48
-
-
Save DarrenSem/4d37711fb71a484b6ed2356c60e3c94a to your computer and use it in GitHub Desktop.
above.js (PARENT element, levels = #) + next.js (SIBLING element, levels = #) helper functions instead of foo.parentElement.parentElement.parentElement etc.
This file contains 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 characters
// 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; | |
while ( count-- && (el = el?.nextElementSibling) ); | |
return el || null; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment