Problem with events and options in plugin

Problem with events and options in plugin

I'm bumping into some problems in my new plugin which uses events & options.

The plugin attaches a function to the change event of all elements (usually input boxes)  which match the selector criteria. The behaviour of the event code should be influenced by the options which have been used in the plugin call.

The problem is, that if I call the plugin several times with different options, the code in the event section will always use the options from the last time the plugin was called.

The reason for this is very likely that the options are not store and the event code does not have access to the options from the time the plugin was called.

  • How can I achieve the goal of getting the right options when the event is triggered resp. how do I properly store and recall the options from the different plugin calls?
  • Where I find some best practice example for this?


Code to call the plugin:
  1.         $('input.myvalue').lknumeric({minNumber:-5000, maxNumber:50000, decimals:1, allowEmpty:false, defaultTo:0});
  2.         $('input.groupsizefrom').lknumeric({minNumber:0, maxNumber:30, decimals:0});

Plugin Code:
  1. jQuery.fn.lknumeric = function(options)
  2.     {
  3.         var opts = $.extend(jQuery.fn.lknumeric.defaults, options);
  4.         this.each(function()
  5.         {    
  6.             $(this).change(function()
  7.             {
  8.                 .... Code using options here .......... (but its always using the options from the last plugin call)
  9.             });
  10.         });
  11.         return this;
  12.     };
  13. jQuery.fn.lknumeric.defaults = {minNumber: null,
  14.                                 maxNumber: null,
  15.                                 decimals: 0,
  16.                                 allowEmpty: true,
  17.                                 defaultTo:""};

Any hint would be appreciated! Thanks!
















    • Topic Participants

    • tom