Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery's .click - pass parameters to user function

I am trying to call a function with parameters using jQuery's .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.

How can I do this specific thing?

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.

like image 796
Paul Hoffer Avatar asked Jul 17 '10 21:07

Paul Hoffer


People also ask

How to pass parameter in click event?

In order to pass a value as a parameter through the onClick handler we pass in an arrow function which returns a call to the sayHello function. In our example, that argument is a string: 'James': ... return ( <button onClick={() => sayHello('James')}>Greet</button> ); ...

How to pass parameters in jQuery click function?

@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).


2 Answers

For thoroughness, I came across another solution which was part of the functionality introduced in version 1.4.3 of the jQuery click event handler.

It allows you to pass a data map to the event object that automatically gets fed back to the event handler function by jQuery as the first parameter. The data map would be handed to the .click() function as the first parameter, followed by the event handler function.

Here's some code to illustrate what I mean:

// say your selector and click handler looks something like this... $("some selector").click({param1: "Hello", param2: "World"}, cool_function);  // in your function, just grab the event object and go crazy... function cool_function(event){     alert(event.data.param1);     alert(event.data.param2); } 

I know it's late in the game for this question, but the previous answers led me to this solution, so I hope it helps someone sometime!

like image 136
Chris Kempen Avatar answered Sep 18 '22 12:09

Chris Kempen


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 } 
like image 22
Nick Craver Avatar answered Sep 17 '22 12:09

Nick Craver