How to declare functions of jQuery

Do you need to define native jQuery functions to be available in all elements or selectors? Are you curious? It is simple and versatile. Let’s see how to declare functions of jQuery

For example, say I want to do a function named loading() that shows an animated icon loading on any element.

Function definition

jQuery.fn.loading = function() { 
  $(this[0]).html("<img src='loading.gif'>"); 

Note that this[0] is the element which is invoked in the function, only the first if it's an elements array.

If you want to handle all elements, just use this.



David Burgos

Read more posts by this author.