Skip to content

Instantly share code, notes, and snippets.

@rubinchyk
Last active September 27, 2024 20:10
Show Gist options
  • Select an option

  • Save rubinchyk/42cc2c70280f51c1245a193576350369 to your computer and use it in GitHub Desktop.

Select an option

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