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 Cinnabar
  • 10,840
  • 15
  • 54
  • 88

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
  • 413,451
  • 75
  • 717
  • 787
2

Do this:

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

myObj[fn]();

Cheers

Edgar Villegas Alvarado
  • 17,924
  • 2
  • 41
  • 61