views:

52

answers:

2

how can i write a plugin/function and then be able to call it like this(no selector required)

$.function()

noe i write my plugins something like this:

(function($){
     $.fn.extend({
          //function name
          myFunction : function(){
           //...........
          }
     });
})(jQuery);
+2  A: 

Simply like this:

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

Or just:

jQuery.myFunction = function() { ... };
Greg
+1  A: 

While the "extend" jquery function is the correct way to extend the library, it has two forms.

$.fn.extend which is what you are using in your example is used to add additional functions to actual DOM objects. So for example your "myFunction" function could be used like this if you wanted to take an action on the "document" object in the dom. $(document).myFunction()

To extend the static namespace of jQuery, you need to use the $.extend function instead (note the lack of fn)

(function($){
 $.extend({
      //function name
      myFunction : function(){
       //...........
      }
 });
})(jQuery);

should be what you are looking for.

emills