neelmeg
neelmeg

Reputation: 2509

not able to call a function within a simple jquery plugin

I'm trying to call a function within a simple jquery plugin. I see that the init method adds 30 to the width but the add50 method throws error Uncaught TypeError: Object [object Object] has no method 'add50'. Not sure what is causing this error to occur.

jsfiddle: http://jsfiddle.net/nsshrinivasan/KnDMq/6/

;(function ( $, window, document, undefined ) {

    // Create the defaults once
    var pluginName = 'Extender',
        defaults = {
            propertyName: "value"
        };

    // The actual plugin constructor
    function Plugin( element, options ) {
        this.element = element;
        this.options = $.extend( {}, defaults, options) ;
        this._defaults = defaults;
        this._name = pluginName;
        this.init(this.element);
    }

    Plugin.prototype = {
        init : function (element) {
           // console.log($(this.element).width());
           $(element).width($(element).width() + 30);
        },
        add50 : function(element){
           $(element).width($(element).width() + 50);
        }

     };

    $.fn[pluginName] = function ( options ) {
        return this.each(function () {
            if (!$.data(this, 'plugin_' + pluginName)) {
                $.data(this, 'plugin_' + pluginName,
                new Plugin( this, options ));
            }
        });
    }

})( jQuery, window, document );

$('#metal').Extender().add50();    

HTML:

<div id='metal'></div>

CSS:

#metal{
    width: 50px;
    height: 10px;
    background:red;
}

Upvotes: 2

Views: 643

Answers (2)

James Kleeh
James Kleeh

Reputation: 12228

I updated your fiddle: Fiddle

;(function ( $, window, document, undefined ) {

    // Create the defaults once
    var pluginName = 'Extender',
        defaults = {
            propertyName: "value"
        };

    // The actual plugin constructor
    function Plugin( element, options ) {
        this.element = element;
        this.options = $.extend( {}, defaults, options) ;
        this._defaults = defaults;
        this._name = pluginName;
        this.init();
    }

    Plugin.prototype = {
        init : function () {
           // console.log($(this.element).width());
           $(this.element).width($(this.element).width() + 30);
        },
        add50 : function(){
           $(this.element).width($(this.element).width() + 50);
        }

     };

    $.fn[pluginName] = function ( options ) {
        return $.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
    }

})( jQuery, window, document );

$('#metal').Extender().add50();   

Upvotes: 1

Syon
Syon

Reputation: 1614

Try adding something like this:

function Plugin( element, options ) {
    this.element = element;
    this.options = $.extend( {}, defaults, options) ;
    this._defaults = defaults;
    this._name = pluginName;
    if (this[options]){
        this[options](this.element);
    } else {
        this.init(this.element);
    }
}

Then you can all your plugin like this: $('#metal').Extender('add50'); and pass in your method as an option. jsFiddle Update

Upvotes: 1

Related Questions