nick
nick

Reputation: 3711

jQuery plugin, with or without a selector

I've created a nice jQuery plugin that looks something like this:

  ;(function($, window, document){

  // MyPlugin stuff

  $.fn.myPlugin = function(){

      new MyPlugin(this).init();

  }

  )(jQuery, window, document);

Works great when called on an element like this: $('.something').myPlugin(); but I'd like to also be able to do this $.myPlugin.somemethod() (use it without an jQuery selector).

How would I do that? Thanks!

Upvotes: 0

Views: 402

Answers (1)

adeneo
adeneo

Reputation: 318182

By not attaching it to jQuery's prototype (fn), but as a property of jQuery instead, or in addition to the plugin etc :

$.myPlugin = {
    somemethod : function() {
        alert('stuff');
    }
}

Upvotes: 2

Related Questions