Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: what does the (e) in function (e) stand for? [duplicate]

Tags:

jquery

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");
});
like image 578
bigpotato Avatar asked Dec 27 '22 18:12

bigpotato


2 Answers

e is an event object, it contains event data. Refer to jQuery Event Type, where you can find description of attributes and methods:

Attributes:

  1. event.type
  2. event.target
  3. event.data
  4. event.relatedTarget
  5. event.currentTarget
  6. event.pageX/Y
  7. event.result
  8. event.timeStamp

Methods:

  1. event.preventDefault()
  2. event.isDefaultPrevented()
  3. event.stopPropagation()
  4. event.isPropagationStopped()
  5. event.stopImmediatePropagation()
  6. event.isImmediatePropagationStopped()
like image 162
Zbigniew Avatar answered Dec 29 '22 09:12

Zbigniew


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 in event.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 matching selector. (Note that this may not be equal to event.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.

like image 24
Felix Kling Avatar answered Dec 29 '22 11:12

Felix Kling