I had a generic task in my project which required changing some elements. Instead of making changes for each element I decided to create a plugin for the common task and then bind plugin with each element.

I had no prior experience of creating plugin so followed an example on http://learn.jquery.com/plugins/basic-plugin-creation/ and created my plugin. Well, it worked great when I initialized plugin on only one element but everything broke down when attached plugin on multiple elements. For example consider the following code

(function ($) {
  $.fn.myplugin = function (defaults) {

    var options = {};

    var selectedId = null;

    var select = this;


In above code, I need all properties to be related to specific element but they are appearing to be shared on global level or they are may be overwritten when plugin in initialized more than one time. How can I control scope of my plugin properties?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.