user1331131
user1331131

Reputation: 427

Select jQuery plugin dynamically

I have a set of jquery plugins as follows:-

fooADD(options)
fooSUB(options)
fooDIV(options)
....

I then have a plugin that acts on a with an attribute data-plugin=""

So for:-

<DIV data-plugin="fooADD">

I want to run the code:-

var item = $('<div/>').fooADD({'option':'value'});

How can I achieve this such that I can keep adding new foo plugins which can be selected from data-plugin without the need to update an array of function lookups?

Upvotes: 2

Views: 79

Answers (1)

Adil Shaikh
Adil Shaikh

Reputation: 44740

Demo --> http://jsfiddle.net/zYfWc/

var plugin = $('div').data('plugin');
var item = $('<div/>')[plugin]({'option':'value'});

Upvotes: 4

Related Questions