I love jQuery like the next front-end dev, but sometimes it’s a waste to load the library just for a couple of things.
The other day, I encountered a need to find an element’s third or fourth parent element, far beyond the first level that
.parentNode() returns. Chaining seven
.parentNode()s together would’ve sufficed, but I wanted a more elegant solution. Because classy.
What I came up with was this function, that will move up the DOM until it finds (and returns) a matching parent element.