I have the following structure
// script1.js
jQuery(document).ready(function($) {
var somevar;
$('somelem').myPlugin();
});
// script2.js
(function($) {
$.fn.myPlugin = function(options) {
// access and modify 'somevar' here so that it gets modified
// in the function which called a plugin
};
});
When you pass a primitive type, it is passed by value. But, if you pass an object then it'll pass by reference. So, you can do that -
jQuery(document).ready(function($) {
var somevar = {val: 5};
$(document).myPlugin(somevar);
alert(somevar.val);
});
// script2.js
(function($) {
$.fn.myPlugin = function(options) {
options.val ++;
// access and modify 'somevar' here so that it gets modified
// in the function which called a plugin
};
})(jQuery);
see the live demo :: http://jsfiddle.net/rifat/EdRFm/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With