Namespaced events: add and remove safely
Give your handlers a namespace so you can remove just yours, without touching other scripts.
jQuery · Namespaced events: add and remove safelyIntermediate
function enable() {
$(window).on('resize.myApp', function () { console.log('resized'); });
$(document).on('keydown.myApp', function (e) { console.log(e.key); });
}
function disable() {
$(window).off('.myApp'); // removes every handler with this namespace
$(document).off('.myApp');
}
enable();
disable();offnamespaceeventscleanup