views:

578

answers:

2

With jQuery code like:

$("#myid").click(myfunction);

function myfunction(arg1, arg2)
{/* something */}

How do I pass arguments to myfunction while using jQuery???

+3  A: 

Do it like so

$("#myid").click(function() {

    myfunction(arg1, arg2);
});

This create an anonymous function, which is called when you run the click event.

alex
good! should have thought of that... thanks.
Edward Wong Hau Pepelu Tivrusk
A: 

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));
Breton