How to call a jQuery plugin without specifying any elements?

To call a jQuery plugin without specifying any elements, use the extend method. This approach allows you to create utility functions that operate at the jQuery object level rather than on specific DOM elements.

There are two main ways to extend jQuery functionality:

  • $.fn.extend() ? Adds methods to jQuery objects (element collections)
  • $.extend() ? Adds utility methods directly to the jQuery namespace

Example

Here's a snippet showing how to call a jQuery plugin without specifying any elements ?

// Extending jQuery prototype (for element collections)
$.fn.extend({
   myFunction1: function() {
      return "Function 1 called on elements";
   }
});

// Extending jQuery namespace (utility functions)
$.extend({
   myFunction2: function() {
      return "Function 2 called as utility";
   },
   
   calculateSum: function(a, b) {
      return a + b;
   }
});

// Usage examples
console.log($.myFunction2()); // Called without elements
console.log($.calculateSum(5, 3)); // Utility function usage

The output of the above code is ?

Function 2 called as utility
8

In the example above, myFunction2 and calculateSum are called without specifying any elements, using jQuery's extend method to create namespace-level utility functions.

Conclusion

Using $.extend() allows you to create jQuery utility functions that operate independently of DOM elements, providing a clean way to add global functionality to your jQuery applications.

Updated on: 2026-03-13T17:43:51+05:30

338 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements