Skip to content

Instantly share code, notes, and snippets.

@obeattie
Created February 11, 2010 11:28
Show Gist options
  • Save obeattie/301435 to your computer and use it in GitHub Desktop.
Save obeattie/301435 to your computer and use it in GitHub Desktop.
Element.implement({
closest: function(selector){
/* Matches the nearest element with the passed selector -- much like
* Element.getParent, but with the starting element included */
return (this.match(selector)) ? this : this.getParent(selector);
},
addLiveEvent: function(eventType, selector, handler){
/* Binds a 'live event' to all the element. Whenever the event is allowed to bubble
* up to this element, event.target is checked to see whether it matches the passed
* selector. If it does, the handler will be fired */
this.addEvent(eventType, (function(event){
var target = event.target;
var matched = target.closest(selector);
if (matched) handler.run([event], matched);
}).bindWithEvent(this, selector, handler));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment