Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use JavaScript string as function name?

Tags:

javascript

I need to call a function in the following fashion:

var f = 'fadeOut';
$(v).f(s, function(){
    $(v).css('visibility', 'hidden').css('position', 'absolute');
});

Is this possible to do simply?

like image 645
nick Avatar asked Apr 06 '12 04:04

nick


People also ask

How can I call a function given its name as a string?

var function_name = "string"; function_name = window[function_name];

How do you call a JavaScript function name?

The JavaScript call() Method The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.

How do you call a function name?

Define a function named "myFunction", and make it display "Hello World!" in the <p> element. Hint: Use the function keyword to define the function (followed by a name, followed by parentheses). Place the code you want executed by the function, inside curly brackets. Then, call the function.

How do I turn a string into a function?

To convert a string in to function "eval()" method should be used. This method takes a string as a parameter and converts it into a function.


1 Answers

Yes

var f = 'fadeOut';
$(v)[f](s, function(){
    $(v).css('visibility', 'hidden').css('position', 'absolute');
});
like image 191
Quintin Robinson Avatar answered Sep 21 '22 12:09

Quintin Robinson