Created
January 16, 2016 05:49
-
-
Save tevko/f1b308296d5ec019e889 to your computer and use it in GitHub Desktop.
traverse dom upwards to find an element by it's data attribute
This file contains hidden or 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
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