In Javascript/jQuery what does (e) mean?
Asked Answered
T

11

120

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?

Treaty answered 25/4, 2012 at 20:39 Comment(0)
A
121

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

Arabele answered 25/4, 2012 at 20:40 Comment(5)
lol, this almost seems like a silly question, but never the less, it's in the right place., You might want to further you answer with an example of its use compared to regular js (not that there is a dif, but how it's used in jQuery in similarity to js)Archfiend
@Archfiend Agreed. Planning to do so.Arabele
@SelvakumarArumugam So I'm not so sure they are the same thing. It seems one returns w.Event and the other returns MouseEvent. For the example above I think they are similar enough, but in the case of $(document).on('click', '.btn', e=> { console.log(e); console.log(event)}); the two are different. Specifically for e.currentTarget and event.currentTarget. In this case e.currentTarget will give you what you need, but event.currentTarget returns document and not the button you clicked on.Spillman
Looking at the responses I would say that event === e.originalEvent, and that e contains a bit more detail.Spillman
learn.jquery.com/events/introduction-to-events/…Spillman
B
61

DISCLAIMER: This is a very late response to this particular post but as I've been reading through various responses to this question, it struck me that most of the answers use terminology that can only be understood by experienced coders. This answer is an attempt to address the original question with a novice audience in mind.

Intro

The little '(e)' thing is actually part of broader scope of something in Javascript called an event handling function. Every event handling function receives an event object. For the purpose of this discussion, think of an object as a "thing" that holds a bunch of properties (variables) and methods (functions), much like objects in other languages. The handle, the 'e' inside the little (e) thing, is like a variable that allows you to interact with the object (and I use the term variable VERY loosely).

Consider the following jQuery examples:

$("#someLink").on("click", function(e){ // My preferred method
    e.preventDefault();
});

$("#someLink").click(function(e){ // Some use this method too
    e.preventDefault();
});

Explanation

  • "#someLink" is your element selector (which HTML tag will trigger this).
  • "click" is an event (when the selected element is clicked).
  • "function(e)" is the event handling function (on event, object is created).
  • "e" is the object handler (object is made accessible).
  • "preventDefault()" is a method (function) provided by the object.

What's happening?
When a user clicks on the element with the id "#someLink" (probably an anchor tag), call an anonymous function, "function(e)", and assign the resulting object to a handler, "e". Now take that handler and call one of its methods, "e.preventDefault()", which should prevent the browser from performing the default action for that element.

Note: The handle can pretty much be named anything you want (i.e. 'function(billybob)'). The 'e' stands for 'event', which seems to be pretty standard for this type of function.

Although 'e.preventDefault()' is probably the most common use of the event handler, the object itself contains many properties and methods that can be accessed via the event handler.

Some really good information on this topic can be found at jQuery's learning site, http://learn.jquery.com. Pay special attention to the Using jQuery Core and Events sections.

Bauske answered 22/9, 2016 at 22:26 Comment(0)
A
31

e doesn't have any special meaning. It's just a convention to use e as function parameter name when the parameter is event.

It can be

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

as well.

Adenoma answered 25/4, 2012 at 20:45 Comment(0)
S
8

In that example, e is just a parameter for that function, but it's the event object that gets passed in through it.

Superstition answered 25/4, 2012 at 20:44 Comment(0)
U
7

The e argument is short for the event object. For example, you might want to create code for anchors that cancels the default action. To do this you would write something like:

$('a').click(function(e) {
    e.preventDefault();
}

This means when an <a> tag is clicked, prevent the default action of the click event.

While you may see it often, it's not something you have to use within the function even though you have specified it as an argument.

Unshaped answered 25/4, 2012 at 20:47 Comment(0)
B
5

In jQuery e short for event, the current event object. It's usually passed as a parameter for the event function to be fired.

Demo: jQuery Events

In the demo I used e

$("img").on("click dblclick mouseover mouseout",function(e){
        $("h1").html("Event: " + e.type);
}); 

I may as well have used event

 $("img").on("click dblclick mouseover mouseout",function(event){
            $("h1").html("Event: " + event.type);
    }); 

Same thing!

Programmers are lazy we use a lot of shorthand, partly it decreases our work, partly is helps with readability. Understanding that will help you understand the mentality of writing code.

Brunildabruning answered 28/10, 2015 at 17:39 Comment(0)
H
4

Today I just wrote a post about "Why do we use the letters like “e” in e.preventDefault()?" and I think my answer will make some sense...

At first,let us see the syntax of addEventListener

Normally it will be: target.addEventListener(type, listener[, useCapture]);

And the definition of the parameters of addEventlistener are:

type :A string representing the event type to listen out for.

listener :The object which receives a notification (an object that implements the Event interface) when an event of the specified type occurs. This must be an object implementing the EventListener interface, or a JavaScript function.

(From MDN)

But I think there is one thing should be remarked: When you use Javascript function as the listener, the object that implements the Event interface(object event) will be automatically assigned to the "first parameter" of the function.So,if you use function(e) ,the object will be assigned to "e" because "e" is the only parameter of the function(definitly the first one !),then you can use e.preventDefault to prevent something....

let us try the example as below:

<p>Please click on the checkbox control.</p>
<form>
    <label for="id-checkbox">Checkbox</label>
    <input type="checkbox" id="id-checkbox"/>

    </div>
</form>
<script>
    document.querySelector("#id-checkbox").addEventListener("click", function(e,v){
         //var e=3;
         var v=5;
         var t=e+v;
         console.log(t);
        e.preventDefault();

    }, false);
</script>

the result will be : [object MouseEvent]5 and you will prevent the click event.

but if you remove the comment sign like :

<script>
       document.querySelector("#id-checkbox").addEventListener("click", function(e,v){
            var e=3;
            var v=5;
            var t=e+v;
            console.log(t);
            e.preventDefault();

       }, false);
   </script>

you will get : 8 and an error:"Uncaught TypeError: e.preventDefault is not a function at HTMLInputElement. (VM409:69)".

Certainly,the click event will not be prevented this time.Because the "e" was defined again in the function.

However,if you change the code to:

<script>
       document.querySelector("#id-checkbox").addEventListener("click", function(e,v){
            var e=3;
            var v=5;
            var t=e+v;
            console.log(t);
            event.preventDefault();

       }, false);
   </script>

every thing will work propertly again...you will get 8 and the click event be prevented...

Therefore, "e" is just a parameter of your function and you need an "e" in you function() to receive the "event object" then perform e.preventDefault(). This is also the reason why you can change the "e" to any words that is not reserved by js.

Hypoglossal answered 19/4, 2017 at 9:32 Comment(0)
D
2

It's a reference to the current event object

Darien answered 25/4, 2012 at 20:41 Comment(0)
L
1

this will be my first stackoverflow help but I am confident that my answer will help anyone reading this.

Basically, e is just an object containing information about the EVENT which has just occured. if it is 'click', then the object will contain about the click, if it is 'submit', then the object will contain about the submit, and they are typically found in addEventListener.

clickMe.addEventListener('click', e => {
    console.log(e)
}

meaning, whenever I 'click' the button, it will console.log the INFOMRATION about the event that happened which is I did is to 'click' it, this will print information about the click event. e is very useful because you can access and use the event to your very own project such as printing the location of x value... like

clickMe.addEventListener('click', e => {
    console.log(e.clientX)
} 

then it will print the location where you 'click' that event.. mine it returns 32

if you prefer video, please watch this https://www.youtube.com/watch?v=_BVkOvpyRI0 video is not mine

upvoting me will truly help me since I am a student and looking for opportunity to help here. Love lots!

Loganiaceous answered 5/7, 2022 at 8:0 Comment(0)
S
0

Another answer for those with less experience of coding:

The key to understanding it is not to focus on the (e). The (e) could be named anything else, rather the rule is that one parameter is expected of this particular function because this function is triggered by an event.

So this function triggered by an event expects an event as its parameter.

See eventListener callback https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#the_event_listener_callback

Slipnoose answered 16/8, 2023 at 3:39 Comment(0)
M
-1
$(this).click(function(e) {
    // does something
});

In reference to the above code
$(this) is the element which as some variable.
click is the event that needs to be performed.
the parameter e is automatically passed from js to your function which holds the value of $(this) value and can be used further in your code to do some operation.

Mournful answered 8/8, 2019 at 12:35 Comment(1)
e on a click handler contains the javascript event of the clicked element and not the value of $(this).Brion

© 2022 - 2024 — McMap. All rights reserved.