Skip to content

Instantly share code, notes, and snippets.

@tevko
Created January 16, 2016 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tevko/f1b308296d5ec019e889 to your computer and use it in GitHub Desktop.
Save tevko/f1b308296d5ec019e889 to your computer and use it in GitHub Desktop.
traverse dom upwards to find an element by it's data attribute
var findParentAttribute = function(id, ctx) {
if (ctx.hasAttribute(id)) {
return ctx.getAttribute(id);
} else {
return this.findParentAttribute(id, ctx.parentElement)
}
return false
}
findParentAttribute('foo', document.querySelector('div').parentElement) // will traverse dom to find parent element with data attribute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment