4

Let's say we have the following jQuery plugins

$.accordion();
$.button();

and in a script we have the following code:

var plg = "accordion";

$('selector').plg();

plg = 'button';

$('selector').plg();

The above example, ofcource doesn't work. But is there a way to do something similar, without the usage of eval() ?

Can I execute jQuery plugins from variable ?

2 Answers 2

5

Sure you can!

var plg = "accordion";

$('selector')[plg]();
Sign up to request clarification or add additional context in comments.

1 Comment

That was the correct answer :) It works fine ! I will check it as correct in 3 minutes :) Thanks a lot
2

If what you want is to make an alias for that function you can just assign it, no need for a string or eval:

var plg = $.accordion;

$('selector').plg();

3 Comments

This is not wat I need. I want to run my plugin from a variable name. In example var plg = "pluginname"; $('selector').plg(); Is it posible to make the same thing like var plg = $."pluginname"; ?
Mmmm, it wasn't very clear by your question, but roXon's answer seems to be what you're looking for.
Thanks for your response anyway :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.