Jquery $. Function ()

How can I write a plugin / function and then be able to call it (no selector required)?

$.function() 

Now I write my plugins like this:

 (function($){ $.fn.extend({ //function name myFunction : function(){ //........... } }); })(jQuery); 
+4
source share
2 answers

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

$. fn.extend, which is what you use in your example, is used to add extra functions to real DOM objects. For example, your "myFunction" function can be used this way if you want to perform an action on the "document" object in dom. $ (Document) .myFunction ()

To expand the jQuery static namespace, 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.

+10
source

Just:

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

Or simply:

 jQuery.myFunction = function() { ... }; 
+7
source

All Articles