Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: parameters for unnamed functions

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
            });
        });
    });
like image 363
Wei Avatar asked Jul 16 '26 17:07

Wei


2 Answers

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.

like image 199
Jeff M Avatar answered Jul 18 '26 07:07

Jeff M


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.

like image 43
mbillard Avatar answered Jul 18 '26 07:07

mbillard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!