views:

16

answers:

0

I'm trying to build a jQuery plugin for a form, and I'm having some serious difficult wrapping my head around the scope. I was able to get a simple test case working, but since then I've hit a bit of a wall. I am trying to do the following:

  • Execute the plugin on multiple forms on 1 page (currently, the settings variable gets overridden on the second call)
  • Be able to call a function on the plugin that will modify all of the 'items' in the form (clearAll() below)
  • Be able to call a function on the plugin that will modify a single element (_clearItem() below)

I'm obviously doing something wrong, I just don't know what. Definitely would appreciate a point in the right direction.

(function($) {

    var items = new Array();
    var settings;
    var $form;
    var dirty = false;

  $form = $.fn.formHelper = function(options) {
        settings = $.extend({}, $.fn.formHelper.defaults, options);

        return this.each(function() {
            var item = $(this);
            items.push(item);

            function _clearItem() {
                //clear the item here
            }
        });
  };

    $form.clearAll = function() {
        for(var x=0; x < items.length; x++) {
            //clear each item individually
        }
    };

    $.fn.formHelper.defaults = {
        onModified: false
    };

})(jQuery);