For example I have jquery plugin and I call an init method with some atrributes like this:
jQuery(document).ready(function($) {
$('#someid').somePlugin({
animation : 'fadeIn',
speed : '1000',
easing : 'swing'
});
});
Let’s pretend I have also html link on a page. Is it possible somehow to change attribute values of this plugin’s init function by clicking for example on html link?
So, by clicking on the link I want to change for example speed attribute without page refreshing. If this possible, what is the best way to implement this action?
Or maybe I should change something in my plugin’s code first.
Plugin’s code:
(function($) {
$.fn.extend({
somePlugin: function(options) {
var defaults = {
speed : 500,
animation : 'fadeIn',
easing : false
}
var options = $.extend(defaults, options);
var speed = options.speed;
var easing = options.easing;
var animation = options.animation;
return this.each(function() {
var obj = $(this);
//some action here
});
}
});
})(jQuery);
Yes, you can do it like this:
Additionally, you can change multiple options at once via:
I should mention this only works for certain if you’re using “official” jQuery plugins. A lot of plugin authors adhere to this design pattern, but not all do. Also, if you develop a plugin using the jQuery UI Widget Factory, you get this functionality for free.
EDIT: Since the OP is authoring his own plugin, we can show an example of how to facilitate this functionality with a custom plugin. Taken directly from the jQuery plugin authoring page: