3

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?

  • not sure but maybe you are getting a collection that you need to jquery .each on? http://api.jquery.com/jquery.each/ or even plain js .forEach ? – Michael Durrant Nov 18 '14 at 12:36

0 Answers0