I am using this set-up to allow accessing the variable outside the jQuery plugin:
(function($){
$.fn.myplugin = function() {
return this.each(function() {
// plugin code here
console.log($.fn.myplugin.myvar);
});
};
$.fn.myplugin.myvar = { ... };
})(jQuery);
I can access myvar
inside and outside the plugin using $.fn.myplugin.myvar
. But it's quite verbose... I feel like there must be a way to access the variable inside the plugin with a shorter name, like this.myvar
. That example doesn't work as this
refers to the current element. But is there another way to access the variable more simply?