Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preparing plugin for $.plugin() usage instead of $(selector).plugin()

Tags:

jquery

Simply:

(function($){
    $.fn.plugin = function()
    {
        // results in error
        // when called like
        // $.plugin();
    }
})(jQuery);

The error is (copied from Chrome console):

Uncaught TypeError: Object function ( selector, context ) {
        // The jQuery object is actually just the init constructor 'enhanced'
        return new jQuery.fn.init( selector, context, rootjQuery );
    } has no method 'plugin'

What rules I have to follow in order to make my plugin work without the element selection?

like image 768
tomsseisums Avatar asked Dec 12 '25 15:12

tomsseisums


1 Answers

Simply assign the function directly to $.plugin:

(function($){
    $.plugin = function()
    {
        // ...
    }
})(jQuery);

Of course this won't refer to selected elements anymore, but to jQuery itself.

like image 177
Felix Kling Avatar answered Dec 15 '25 07:12

Felix Kling