1

I was thinking the is it possible to pass a variable to function which is to be executed on clicking the button and the function is given by a variable.

Also the parameters of the functions could be more than one and so I separated them by ,.

So is there a way that I can put the parameters into function using any jQuery plugin or a function. Please help me out, cause I'm confuse big time! Here is my current jQuery Code:

function Func (a,b){
 alert(a+' | '+b);   
}
var Parameters = 'cool,123';
var Function = Func;

$('button').click(Function);

Problem demo

Thanks in advance

4 Answers 4

3

You can use function.apply(). Pass your list of parameters in as an array and you can even set a scope if you wish to do so:

function myFunc(a,b){
    alert(a+", "+b);
}

var args = ["one", "two"];
var func = myFunc;

$('button').click(function(){
    func.apply(this, args);
});
Sign up to request clarification or add additional context in comments.

Comments

2

Is this what you're after?

function Func (a,b){
 alert(a+' | '+b);   
}
var Parameters = 'cool,123';

$('button').click(function() {
    Func.apply(this, Parameters.split(","));
});

Demo: http://jsfiddle.net/6nJx7/14/

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/function/apply

4 Comments

This is the best answer! And it has won the Jack Pot!
The only problem is that you can't pass objects/functions as arguments, only strings which will get automatically typecast to numbers when needed.
@DarthJDG - this is true. I would have preferred if he had accepted your answer, I posted this when you had deleted your initial answer, and before you undeleted.
No problem, I'm not complaining because of that, I just thought I'd mention. It happened to me enough times too that I was just about to post an answer when the message popped up that someone else beat me to it. Annoying that is, but sometimes I just submit anyway. :)
1

You could pass your parameters as an array ..

 function Func (a){
     alert(a[0]+' | '+a[1]);   
    }
    var Parameters = new Array(0,1);
    var Function = Func;

    $('button').click(Function(Parameters));

2 Comments

Thanks but the function is loading itself on refreshing the document! Any suggestions? jsfiddle.net/divinemamgai/MMqM5
That won't work. You'd wind up calling Function(Parameters) immediately, since it is isn't wrapped as a closure. You'd want something more like '$('button').click(function(){Function(Parameters)});' -- However, the 'apply' suggestion below will be more flexible.
1

Here is another way to do it, I like this better myself because it is somewhat self documenting. Each of the "parameters" in the options list has a name.

This is a common design pattern in JavaScript libraries for options to a module.

function Func (c){
 alert(c.a+' | '+c.b);   
}
var Parameters = { a : "cool", b: 123 };
var Function = Func;

$('button').click(Parameters );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.