Calling a Function Based on a String Which Contains the Function Name

Posted by Phonethics on Stack Overflow See other posts from Stack Overflow or by Phonethics
Published on 2010-05-04T06:22:12Z Indexed on 2010/05/04 6:28 UTC
Read the original article Hit count: 181

Filed under:
|
|
|
var foo1,foo2;

switch (fn)
{
    case "fade"  : foo1 = "fadeOut"; foo2 = "fadeIn"; break;                
    case "slide" : foo1 = "slideUp"; foo2 = "slideDown"; break;
}

eval("$('.cls1')." + foo1 + "();");
currentSlideIndex = currentSlideIndex + n;
eval("$('.cls1')." + foo2 + "();");

Any better way to achieve this without using eval ? Im not a very big fan of using eval unless absolutely necessary.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery