tags:

views:

82

answers:

3
$("p").bind("click", function(event){
       // code goes here
});

This is quite understandable. But what is the way to use a non-inline function and pass the event as an argument? That is:

$("p").bind("click", myFunction(event));

 function myFunction(event) {
       // code goes here
 }

Thank you!

+3  A: 

Here goes:

$("p").bind("click", myFunction);

function myFunction(event) {
    // code goes here
}
Justin Niessner
+2  A: 

Your problem is that you are giving it an argument which causes the function to be executed and the return value substituted as the argument to bind. What you want to do is just supply a reference to the function as the argument to bind. Do that by leaving off the parentheses (and the argument). The arguments supplied by bind to the callback only need to be included in the function definition.

$("p").bind("click", myFunction);

function myFunction(event) {
    // code goes here
}
tvanfosson
A: 

So when you do something like

$('p').bind('click', myFunction(event);

function myFunction(event){
//code
}

Is like if you are creating your own 'event object' instead of using the one created by jQuery when the user action occurs?

fabio