How can I pass arguments to event handlers in jQuery?

JavascriptJquery

Javascript Problem Overview


With jQuery code like:

$("#myid").click(myfunction);
            
function myfunction(arg1, arg2) {/* something */}

How do I pass arguments to myfunction while using jQuery?

Javascript Solutions


Solution 1 - Javascript

The simplest way is to do it like so (assuming you don't want any of the event information passed to the function)...

$("#myid").click(function() {
    myfunction(arg1, arg2);
});

jsFiddle.

This create an anonymous function, which is called when the click event is triggered. This will in turn call myfunction() with the arguments you provide.

If you want to keep the ThisBinding (the value of this when the function is invoked, set to the element which triggered the event), then call the function with call().

$("#myid").click(function() {
    myfunction.call(this, arg1, arg2);
});

jsFiddle.

You can't pass the reference directly in the way your example states, or its single argument will be the jQuery event object.

If you do want to pass the reference, you must leverage jQuery's proxy() function (which is a cross browser wrapper for Function.prototype.bind()). This lets you pass arguments, which are bound before the event argument.

$("#myid").click($.proxy(myfunction, null, arg1, arg2));   

jsFiddle.

In this example, myfunction() would be executed with its ThisBinding intact (null is not an object, so the normal this value of the element which triggered the event is used), along with the arguments (in order) arg1, arg2 and finally the jQuery event object, which you can ignore if it's not required (don't even name it in the function's arguments).

You could also use use the jQuery event object's data to pass data, but this would require modifying myfunction() to access it via event.data.arg1 (which aren't function arguments like your question mentions), or at least introducing a manual proxy function like the former example or a generated one using the latter example.

Solution 2 - Javascript

$("#myid").on('click', {arg1: 'hello', arg2: 'bye'}, myfunction);

function myfunction(e) {

    var arg1 = e.data.arg1;
    var arg2 = e.data.arg2;

    alert(arg1);
    alert(arg2);

}

//call method directly:
myfunction({
    arg1: 'hello agian', 
    arg2: 'bye again'
});

Also allows you to bind and unbind specific event handlers using the on and off methods.

Example:

$("#myid").off('click', myfunction);

This would unbind the myfunction handler from #myid

Solution 3 - Javascript

while you should certainly use Alex's answer, the prototype library's "bind" method has been standardized in Ecmascript 5, and will soon be implemented in browsers natively. It works like this:

jQuery("#myid").click(myfunction.bind(this, arg1, arg2));

Solution 4 - Javascript

There are great answers already, but anyway, here are my two cents. You can also use:

$("#myid").click({arg1: "foo", arg2: "bar"}, myfunction)

And the listener would look like:

function myfunction(event){ alert(event.data.arg1); alert(event.data.arg2); }

Solution 5 - Javascript

Old thread, but for search purposes; try:

$(selector).on('mouseover',...);

... and check out the "data" parameter: http://api.jquery.com/on/

e.g.:

function greet( event ) {
  alert( "Hello " + event.data.name );
}
$( "button" ).on( "click", {name: "Karl"}, greet );
$( "button" ).on( "click", {name: "Addy"}, greet );

Solution 6 - Javascript

Simple:

$(element).on("click", ["Jesikka"],  myHandler);

function myHandler(event){
   alert(event.data);     //passed in "event.data"
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEdward Wong Hau Pepelu TivruskView Question on Stackoverflow
Solution 1 - JavascriptalexView Answer on Stackoverflow
Solution 2 - JavascriptGeorge FilippakosView Answer on Stackoverflow
Solution 3 - JavascriptBretonView Answer on Stackoverflow
Solution 4 - JavascriptLeoView Answer on Stackoverflow
Solution 5 - JavascriptdhcView Answer on Stackoverflow
Solution 6 - JavascriptT.ToduaView Answer on Stackoverflow