Last active
March 4, 2020 14:00
-
-
Save astromac/53a04080886a038d01fd1337fe5434ca to your computer and use it in GitHub Desktop.
Traverse through DOM
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
/* Locates the first parent element | |
* that has a given attribute. | |
*/ | |
const findParentElement = (node, attribute) => { | |
if (isEmpty(node) || isEmpty(attribute)) return null; | |
let element = node; | |
/* eslint-disable no-cond-assign, no-empty */ | |
while ( | |
(element = element.parentElement) && | |
(element.getAttribute(attribute) === null) | |
) {} | |
/* eslint-enable no-cond-assign, no-empty */ | |
return element; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment