Skip to content

Instantly share code, notes, and snippets.

@rubinchyk
Created February 7, 2024 11:55
Show Gist options
  • Save rubinchyk/42cc2c70280f51c1245a193576350369 to your computer and use it in GitHub Desktop.
Save rubinchyk/42cc2c70280f51c1245a193576350369 to your computer and use it in GitHub Desktop.
[Search nearest element from another element] Search nearest element from another element
// 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