Re:This Week in Rails: jQuery no longer part of Rails, and more
by dham on Friday December 09, 2016 @07:55AM
(#13138461)
This is what I use in my vanilla js projects for event delegation.
function delegate(el, evt, sel, handler) {
el.addEventListener(evt, function (event) {
let t = event.target
while (t && t !== this) {
if (t.matches(sel)) {
handler.call(t, event)
}
t = t.parentNode
}
})
}
delegate(document, 'click', '[data-behavior="open"]', function (event) {
event.preventDefault()
})
You'll need a matches polyfill if supporting browsers that don't support el.matches
You'll need a matches polyfill if supporting browsers that don't support el.matches