views:

151

answers:

5

just now i ask a question, and it creates a new question.

for example, when i wrote

$("#new_lang").click(function(e) 
{
  alert("something");
  e.stopPropagation();
});

who is e here, and why it doesn't work without it? why can i write anything instead of e?

Thansk

+3  A: 

e in that example is the event object (link to docs) for the click event. As with any function argument, you can use any name you want. If you don't need to do anything with it (for instance, if you don't need to call stopPropagation), you can leave it off entirely.

T.J. Crowder
+4  A: 

It is event object.

http://api.jquery.com/category/events/event-object/

Krunal
Why is this upvoted so much? It only answers 1/3 of the question...
Nick Craver
i've upvoted, because it was the first answer.
Syom
i updated because no need to give longer description it is out of scope and its already written so why to copy and paste
Pranay Rana
@Syon: Amazingly, it was in a tie for first answer -- to the second -- with mine.
T.J. Crowder
@pranay: *"...and its already written so why to copy and paste..."* http://meta.stackoverflow.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers
T.J. Crowder
The *down* votes seem harsh, though.
T.J. Crowder
+7  A: 

e is the event object that the handler receives. You need not use "e" specifically as the variable name, you can call it anything you want (as long as it's not any number of keywords!), many call it event for example.

Yes, you can do without it, since it's the first argument, arguments[0] also works, but I wouldn't go that route. You can see this working here, but again I would just use the argument declared like it is currently so it's very explicit.

Nick Craver
...happy now? :)
galambalazs
@galambalazs - about? I don't care if *my* answer is selected as an answer...I care that a **good** answer is selected. I don't down-vote often (check my profile), didn't here either...but when the next googler comes along looking for an answer to the question, the site is better overall if a **good, complete** one is selected to be at the top for them (via being accepted). I think this, Jamiec and T.J. all fit that description, and I'd be happy if any of them was accepted.
Nick Craver
there is no argument about that... i completely agree with you. :)
galambalazs
+6  A: 

e, in this context, is the event object raised by the click event. It will work perfectly well without it (albeit, in your example you will be unable to stopPropagation):

$("#new_lang").click(function() 
{
  alert("something");
});

You can also substitute any name for e, as you would with any function param

$("#new_lang").click(function(eventObj) 
{
  alert("something");
  eventObj.stopPropagation();
});
Jamiec
A: 

Again from http://api.jquery.com/bind/ as linked on the previous question:

The handler callback function can also take parameters. When the function is called, the JavaScript event object will be passed to the first parameter.

The event object is often unnecessary and the parameter omitted, as sufficient context is usually available when the handler is bound to know exactly what needs to be done when the handler is triggered. However, at times it becomes necessary to gather more information about the user's environment at the time the event was initiated.

There is a lot of useful info on that page. :)

Chris