I'm making a plugin that at some stage runs a function.
Example:
<button class="btn btn-warning btn-lg" id="act"> Action </button>
(function($) {
$.fn.foo = function(options) {
var opts = $.extend({}, $.fn.foo.defaults, options);
this.init = function() {
this.css('background', opts.background);
this.on(opts.action[0],opts.action[1]);
};
this.init();
return this;
};
$.fn.foo.defaults = {
action: ["click", "bar()"],
background: "black"
};
}(jQuery));
function bar() {
console.log('hello,world');
}
$("#act").foo();
I want to run a function out of the scope of the plugin, an example and if I declare $("#act").foo({action:["dblclick","bar()"]});
, how can I run it inside my plugin? I tried this using the new Function (opts.action [1]) too, it says the function is undefined.