javascript - How to call function using the value stored in a variable? -
i have variable
var functionname="givevote";
what need is, want call function stored in var functionname. tried using functionname(); . not working. please help.
edit based on same problem, have
$(this).rules("add", {txtinf: "^[a-za-z'.\s]{1,40}$" });
rules predifined function takes methodname:, here have hardcoded txtinf. want supply javascript variable here, make code generic. var methodname="txtinf";
here want evaluate methodname first before being used in rules function.
$(this).rules("add", {mehtodname: "^[a-za-z'.\s]{1,40}$" });
you have handful of options - 2 basic ones include window[functionname]()
or settimeout(functionname, 0)
. give shot.
edit: if variable string name of function, use those; otherwise, if you're able assign function reference variable (eg, var foo = function() {};
), use foo()
notation, or foo.apply(this, [])
, etc.
edit 2: evaluate methodname
in place prior $(this).rules()
firing, you'd apply first syntax above (using window
object), so:
$(this).rules("add", {window[methodname](): ...});
i think that's you're asking - if not, please clarify bit more , i'll happy rewrite solution.
Comments
Post a Comment