Appendix
A few technical details about jQuery's inner workings
Shorthand Methods
We've already gotten acquainted with the click() shorthand method. In reality, this method is just a wrapper for calling on() and trigger():
jQuery.fn.click = function( data, fn ) {
if (arguments.length > 0) {
this.on("click", fn);
} else {
this.trigger("click");
}
};Oops, I tweaked the code a tiny bit for readability. If curiosity gets the better of you, search the source code for the string "
dblclick"
Event Handlers on Objects
You can attach event handlers to practically any object:
// an object that couldn't be simpler
var obj = {
test:function() {
console.log("obj.test");
}
}
// create a handler for a custom event someEvent
$(obj).on("someEvent", function(){
console.log("obj.someEvent");
this.test();
});
// let's see the result
console.log(obj);
// trigger the someEvent event
$(obj).trigger("someEvent");Copy the code above into the console and run it — I think you'll find it interesting ;)
Where Are Event Handlers Stored?
Inside jQuery itself :) If you want to get all event handlers, you'll need the following method:
Back in the day, all event handlers were available in the
$.cacheobject, but in recent versions of jQuery this object no longer exists :(
Last updated