In Javascript/jQuery what does (e) mean?

shrewdbeans picture shrewdbeans · Apr 25, 2012 · Viewed 136.4k times · Source

I am new to JavaScript/jQuery and I've been learning how to make functions. A lot of functions have cropped up with (e) in brackets. Let me show you what I mean:

$(this).click(function(e) {
    // does something
});

It always appears that the function doesn't even use the value of (e), so why is it there so often?

Answer

Selvakumar Arumugam picture Selvakumar Arumugam · Apr 25, 2012

e is the short var reference for event object which will be passed to event handlers.

The event object essentially has lot of interesting methods and properties that can be used in the event handlers.

In the example you have posted is a click handler which is a MouseEvent

$(<element selector>).click(function(e) {
    // does something
    alert(e.type); //will return you click
}

DEMO - Mouse Events DEMO uses e.which and e.type

Some useful references:

http://api.jquery.com/category/events/

http://www.quirksmode.org/js/events_properties.html

http://www.javascriptkit.com/jsref/event.shtml

http://www.quirksmode.org/dom/events/index.html

http://www.w3.org/TR/DOM-Level-3-Events/#event-types-list