Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function(e){e.something...} What is e?

When I write

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

What is e here, and why doesn't the function work without it? Why can I write anything instead of e?

like image 409
Simon Avatar asked Jun 30 '10 11:06

Simon


2 Answers

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.

like image 157
Nick Craver Avatar answered Oct 06 '22 00:10

Nick Craver


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();
});
like image 21
Jamiec Avatar answered Oct 06 '22 00:10

Jamiec