I have a variable
var functionName="giveVote";
What I need to do is, I want to call function stored in var functionName. I tried using functionName(); . But its not working. Please help.
Edit Based on the same problem, I have
$(this).rules("add", {txtInf: "^[a-zA-Z'.\s]{1,40}$" });
rules is a predifined function which takes methodName:, here I have hardcoded txtInf. But I want to supply a javascript variable here, to make my code generic.
var methodName=”txtInf”;
Here I want to evaluate methodName first before being used in rules function.
$(this).rules("add", {mehtodName: "^[a-zA-Z'.\s]{1,40}$" });
You have a handful of options – two basic ones include
window[functionName]()orsetTimeout(functionName, 0). Give those a shot.Edit: if your variable is just the string name of a function, use those; otherwise, if you’re able to actually assign a function reference to the variable (eg,
var foo = function() {};), you could use thefoo()notation, orfoo.apply(this, []), etc.Edit 2: To evaluate
methodNamein place prior to$(this).rules()firing, you’d just apply the first syntax above (using thewindowobject), like so:I think that’s what you’re asking – if not, please clarify a bit more and I’ll be happy to rewrite a solution.