Kassem
Kassem

Reputation: 8266

Calling a custom jQuery plugin off the jQuery object itself

I am having trouble calling a jQuery plugin off the jQuery object itself. So rather than calling $(selector).myPlugin() I want to call $.myPlugin instead. For some reason it tells me that the function is undefined.

Here's my code:

(function ($) {

    var _current = null;

    var methods = {
        init: function (options) {
            _current = $('.rfgQuestionsWrapper').filter(function () {
                return $(this).css('display') != 'none';
            }).first();
            console.log(_current);
            $('.gaugeTitle').click(function (e) {
                var rfgCode = $(this).parent().find('.gaugeWrapper').attr('id');
                console.log(rfgCode);
                showByCode(rfgCode);
                return false;
            });
        },
        showByCode: function (rfgCode) {
            var target = $.utilities.filterById('.rfgQuestionsWrapper', rfgCode);
            console.log(target);
            _current.hide();
            target.show();
        }
    };

    $.fn.navigationManager = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }
    };

})(jQuery);

I must be doing something wrong because this is the first time I call a plugin this way... Any suggestions?

Upvotes: 0

Views: 87

Answers (1)

Andy
Andy

Reputation: 30135

Look at that question: in jQuery what is the difference between $.myFunction and $.fn.myFunction?

Basically instead of $.fn.navigationManager = function(){} you write $.navigationManager = function(){}.

Upvotes: 1

Related Questions