Skip to content Skip to sidebar Skip to footer

How To Pass Arguments In A Function Reference

I'm looking for a way to save reference to two this objects in one function called after an event triggers in jQuery - this reference to the object the method is defined in (so I c

Solution 1:

Jquery .on event has option to pass the argument as parameter in event handler like this

cell.$el.on('click', 'li.multiselect-option', {arg1:'arg1' , arg2:'arg2'} , myFunction);
...
myClickFunction: function(event)
{
    alert(event.data.arg1);
    alert(event.data.arg2);

    this.anotherFunction();
    $(event.currentTarget).html("Don't click me anymore!!!");
}

Passing data to the handler

If a data argument is provided to .on() and is not null or undefined, it is passed to the handler in the event.data property each time an event is triggered. The data argument can be any type, but if a string is used the selector must either be provided or explicitly passed as null so that the data is not mistaken for a selector. Best practice is to use a plain object so that multiple values can be passed as properties.

or another way

cell.$el.on('click', 'li.multiselect-option',  function() { 

           myClickFunction("hai" , "bye");

});


myClickFunction: function(arg1, arg2)
{
   alert(arg1);

   alert(arg2);   
}

Solution 2:

And I would also suggest a plugin-free solution to the question "how to supply a parameter to function reference"

Example

functionx(a){
    console.log(a);
}

setInterval('x(1)',1000);

Post a Comment for "How To Pass Arguments In A Function Reference"