Created
February 7, 2024 11:55
-
-
Save rubinchyk/42cc2c70280f51c1245a193576350369 to your computer and use it in GitHub Desktop.
[Search nearest element from another element] Search nearest element from another element
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
// example findNextPageBreak(startElement, "page-class") | |
function findNextPageBreak(startElement, classElement) { | |
let currentElement = startElement; | |
while (currentElement) { | |
if (currentElement.nextElementSibling && currentElement.nextElementSibling.classList.contains(classElement)) { | |
return currentElement.nextElementSibling; | |
} | |
currentElement = currentElement.nextElementSibling || currentElement.parentNode; | |
if (currentElement.tagName === 'BODY') { | |
break; | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment