I created a jquery plug-in. See following.
$.fn.changeColor = function(optionOrAction){ if (typeof optionOrAction == 'object'){ $.fn.changeColor.option = $.extend({}, $.fn.changeColor.option, optionOrAction); } if (typeof optionOrAction == 'object' || optionOrAction == ''){ $(this).css('border', '3px solid red'); } if (optionOrAction == 'showOption'){ alert($.fn.changeColor.option.showColor); } } $.fn.changeColor.option = {}; I tried to use it below.
$(document).ready(function(){ $('#test1').changeColor({ showColor: 'red'}); $('#test2').changeColor({ showColor: 'blue'}); $('#showButton').click(function(){ $('#test1').changeColor('showOption'); $('#test2').changeColor('showOption'); }); }); For test1 plugin, I have set red to showColor option. For test2 plugin, I have set blue to showColor option.
When we call the method of test1 and test2, there should be red for test1 and blue for test2.
Currently, the two plugin shows blue in alert.
The problem is that jquery plugin can't maintain data for multi usage. So, How can I change it to main data in plugin?
The problem is that when I call show option method of plug in, the two alerts are blue. How to maintain state in jquery pluginis not clear. Explain the desired behavior.