Skip to content

Instantly share code, notes, and snippets.

View prosellen's full-sized avatar

Peter Rosellen prosellen

  • CGI Deutschland B.V. & Co. KG
  • Düsseldorf, Germany
  • X @p_rosellen
View GitHub Profile
@prosellen
prosellen / delegate-event.js
Last active April 18, 2016 16:48 — forked from Daniel-Hug/delegate-event.js
Vanilla JS equivalent of jQuery's .live(): use event delegation to handle events whose target matches a selector, closest(): get nearest parent element matching selector
/**
* get nearest parent element matching selector
* Will return "false" if "el" is "null" to prevent errors popping up in the console
*/
var closest = (function() {
var el = HTMLElement.prototype;
var matches = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;
return function closest(el, selector) {
return el === null ? false : matches.call(el, selector) ? el : closest(el.parentElement, selector);