foo.closest(bar) for Mootools
element.closest() for MooTools
Here's a sample implementation of the 'closest()' jQuery method, but for MooTools. It takes your first element and them iterates over its own parent until it finds the element you're looking for, returning it.
Code
Element.implement({
closest: function(el) {
var find = this.getElement(el),
self = this;
while (self && !find) {
self = self.getParent();
find = self ? self.getElement(el) : null;
}
return find;
}
});
Usage
var myEl = document.getElement('.foo');
var findUl = myEl.closest('ul');
Written by Ricardo Magalhães
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#