views:

43

answers:

2

I am trying to call a function with parameters using jquery .click, but I can't get it to work.

This is how I want it to work:

$('.leadtoscore').click(add_event('shot'));

which calls

function add_event(event) {
    blah blah blah }

It works if I don't use parameters, like this

$('.leadtoscore').click(add_event);
function add_event() {
    blah blah blah }

But I need to be able to pass a parameter through to my add_event function.

Has somebody done this specific thing before? I know I can use .click(function() { blah }, but I call the add_event function from multiple places and want to do it this way.

Thank you!

+5  A: 

You need to use an anonymous function like this:

$('.leadtoscore').click(function() {
  add_event('shot')
});

You can call it like you have in the example, just a function name without parameters, like this:

$('.leadtoscore').click(add_event);

But the add_event method won't get 'shot' as it's parameter, but rather whatever click passes to it's callback, which is the event object itself...so it's not applicable in this case, but works for many others. If you need to pass parameters, use an anonymous function...or, there's one other option, use .bind() and pass data, like this:

$('.leadtoscore').bind('click', { param: 'shot' }, add_event);

And access it in add_event, like this:

function add_event(event) {
  //event.data.param == "shot", use as needed
}
Nick Craver
When you say just, does that mean only? Secondly, does that mean I would be able to use $(this) inside my add_event function like I normally would inside the anonymous function?
phoffer
@phoffer - Yes, "just" means no params, *only* the function name, it's a reference to the function, not the result of running the function you want to assign to the `click` handler. In the anonymous method and `.bind()` examples above you can use `this`, it'll refer to the `.loadtoscore` you clicked on (like you'd expect). In the last example, inside `add_event(event)` the same is true, use `this` or `$(this)` and it'll be what you want.
Nick Craver
@phoffer: You would have to pass the element explicitly in order to use it, e.g.: `function(){ add_event('shot', $(this));}` and `function add_event(event, element){...}`. `element` would be a jQuery element here (it works with `bind()` though as Nick mentioned).
Felix Kling
OK thanks guys! That's what I need to know.
phoffer
@Felix - He can also use `$(this)` inside the function, for example: http://jsfiddle.net/tSu5t/
Nick Craver
@Nick: I meant in this case: http://jsfiddle.net/tSu5t/1/
Felix Kling
+3  A: 

If you call it the way you had it...

$('.leadtoscore').click(add_event('shot'));

...you would need to have add_event() return a function, like...

function add_event(param) {
    return function() {
                // your code that does something with param
                alert( param );
           };
}

The function is returned and used as the argument for .click().

patrick dw
+1, nice one. working example http://www.jsfiddle.net/naRRk/
jAndy
@jAndy - Thanks for posting the example including the `event` parameter. I should have included it. :o)
patrick dw
http://www.jsfiddle.net/naRRk/1/ just make things clear
jAndy