views:

637

answers:

3

In the following jQuery JavaScript code, what value does the parameter "e" take on within the function? I'm having difficulty understanding this because this function cannot be passed an argument elsewhere in the code so how would having a parameter work? And how would I use parameters in such functions that are not named and not called anywhere else in the code?

 $(document).ready( function() { 
  $('div').each(function() {
   $(this).click(function(e){
    //some code
   });
  });
 });
+6  A: 

click sets the event handler. The click handler gets called by the browser when the event occurs, and the e parameter contains information about that event.

For keypress events, it contains which keys were pressed and what modifiers were pressed at that time (shift, control, etc.).

For mouse events, it contains the position of the click and which button was used.

See http://www.quirksmode.org/js/events_properties.html for more information about the properties of the event structure.

Jeff M
+1  A: 

That anonymous function is called when the event is fired, and e is an eventObject:

click( fn )

// fn, a function to bind to the click event on each of the matched elements.
function callback(eventObject) {
  this; // dom element
}
CMS
+2  A: 

e is an eventObject as you can see in the jQuery click documentation.

I do not know what you can do with it however, but it should contain information about the click event. Maybe it's the standard DOM event.

GoodEnough