Sometimes you want to switch off all previously registered listeners.
//Adding a normal click handler
$(document).on("click",function(){
console.log("Document Clicked 1")
});
//Adding another click handler
$(document).on("click",function(){
console.log("Document Clicked 2")
});
//Removing all registered handlers.
$(document).off("click")
An issue with this method is that ALL listeners binded on document
by other plugins etc would also be removed.
More often than not, we want to detach all listeners attached only by us.
To achieve this, we can bind named listeners as,
//Add named event listener.
$(document).on("click.mymodule",function(){
console.log("Document Clicked 1")
});
$(document).on("click.mymodule",function(){
console.log("Document Clicked 2")
});
//Remove named event listener.
$(document).off("click.mymodule");
This ensures that any other click listener is not inadvertently modified.