Skip to content

Instantly share code, notes, and snippets.

@roboshoes
Last active March 7, 2017 03:36
Show Gist options
  • Save roboshoes/9519ccdf92333d78d9bf6ce5e2b1ffd8 to your computer and use it in GitHub Desktop.
Save roboshoes/9519ccdf92333d78d9bf6ce5e2b1ffd8 to your computer and use it in GitHub Desktop.
Find attribute in node or parent.
// find attribute
export function findInParent( node, attribute, levels = Infinity ) {
return node.getAttribute( attribute ) ||
( levels > 0 ? findInParent( node.parentElement, attribute, --levels ) : null );
}
// find anything
export function findInParent( node, filter, levels = Infinity ) {
return filter( node ) ||
( levels > 0 ? findInParent( node.parentElement, filter, --levels ) : null );
}
@roboshoes
Copy link
Author

@hapticdata ... very wise Mr Data. Adjusted as suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment