Possible Duplicate:
In Javascript/jQuery what does (e) mean?
I'm new to programming. What exactly is the "e" in function(e) in the code below? I'm guessing it's whatever the recipient of the method is, and what "this" refers to? Just a wild guess. But even so, wouldn't we be able to leave function(e) as function() without the "e"?
$("#" + id).live("mouseenter", function (e) {
$(this).toggleClass("twittereyesopen");
});
e
is an event object
, it contains event data. Refer to jQuery Event Type, where you can find description of attributes and methods:
Attributes:
Methods:
e
is just a parameter name. You can name it however you want to. It will refer to the first argument passed to the function, which in this case will be the event object:
When the browser triggers an event or other JavaScript calls jQuery's
.trigger()
method, jQuery passes the handler an event object it can use to analyze and change the status of the event. This object includes a normalized subset of data provided by the browser; the browser's unmodified native event object is available inevent.originalEvent
.
What this
refers to depends on how a function is called. In jQuery, this
inside event handlers refers to the element the handler is (logically) bound to:
When jQuery calls a handler, the
this
keyword is a reference to the element where the event is being delivered; for directly bound events this is the element where the event was attached and for delegated events this is an element matchingselector
. (Note that this may not be equal toevent.target
if the event has bubbled from a descendant element.) To create a jQuery object from the element so that it can be used with jQuery methods, use$(this)
.
MDN provides a good explanation of this
in general.
You can find more information in the .on
documentation.
But even so, wouldn't we be able to leave function(e) as function() without the "e"?
Yes of course, in JavaScript you can define functions without parameters, but passing arguments to them won't throw an error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With