2

How to call jQuery function by its name. For example:

var fn = 'hide',
    myObj = $("#smth");

// here I want to hide myObj ( $("#smth").hide() )

// my variants were:
// fn.call(myObj) - doesn't work
// myObj.fn() - doesn't work (I've not expected, just tried =) )
Larry Foobar
  • 11,092
  • 15
  • 56
  • 89

2 Answers2

5

Access the function as you would access any other member of myObj using a variable name, and then simply call it:

var fn = 'hide',
myObj = $("#smth");
(myObj[fn])();
Jon
  • 428,835
  • 81
  • 738
  • 806
2

Do this:

var fn = 'hide',
myObj = $("#smth");

myObj[fn]();

Cheers

Edgar Villegas Alvarado
  • 18,204
  • 2
  • 42
  • 61