Instantly share code, notes, and snippets.

@jacobwgillespie jacobwgillespie/cart.js Secret
Created Aug 5, 2016

Embed
What would you like to do?
document.addEventListener('turbolinks:load', function() {
live('select', 'change', function() {
var event = document.createEvent('HTMLEvents');
event.initEvent('submit', true, false);
this.form.dispatchEvent(event);
}, '.cart');
});
function live(selector, eventName, callback, scope) {
scope = scope ? document.querySelector(scope) : document;
if (!scope) return;
scope.addEventListener(eventName, function(event) {
var qs = scope.querySelectorAll(selector);
if (!qs) return;
var el = event.target;
var index = -1;
while (
el &&
((index = Array.prototype.indexOf.call(qs, el)) === -1)
) {
el = el.parentElement;
}
if (index > -1) callback.call(el, event);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment