1

I want to implement multiple jQuery plugins for a small application. I have the list of plugins names and the plugins.

How can I call a plugin having its name stored in a variable?

So, instead of this:

$("div#exercise-container").demo_plugin_for_exercise1();

$("div#exercise-container").demo_plugin_for_exercise2();

I want to have a

var plugin_name = "demo_plugin_for_exercise1";

and use it to call the plugin with that name.

GhitaB
  • 3,275
  • 3
  • 33
  • 62
  • I will have a list of exercise types - and a plugin for each one. The details about each plugin will be stored in json format. Then when I choose an exercise I will know the plugin name to be used... that plugin_name variable. Yes, I can just say if plugin_name == "something" then call something(), but I think there is a better way. – GhitaB Nov 24 '20 at 20:31

1 Answers1

1

It's working like here:

$("div#exercise-container")[plugin_name]();
GhitaB
  • 3,275
  • 3
  • 33
  • 62