Skip to content

Instantly share code, notes, and snippets.

@astromac
Last active March 4, 2020 14:00
Show Gist options
  • Save astromac/53a04080886a038d01fd1337fe5434ca to your computer and use it in GitHub Desktop.
Save astromac/53a04080886a038d01fd1337fe5434ca to your computer and use it in GitHub Desktop.
Traverse through DOM
/* 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