jQuery sub() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Utilities

Definition

jQuery.sub() (alias $.sub) created a new copy of jQuery whose methods you could change without affecting the global jQuery / $ object. It took no arguments and returned that copy (jQuery 1.5–1.8).

Syntax

Start with these common calls. Keep them short and readable.

jQuery
var sub$ = jQuery.sub();   // copy of jQuery

sub$.fn.myCustomMethod = function () {
  return "just for me";
};

sub$("body").myCustomMethod();     // "just for me"
typeof jQuery("body").myCustomMethod;  // undefined

// Not a sandbox — events / Ajax / data still use main jQuery
CallWhat it does
jQueryLearn jQuery.sub(): create a modifiable copy of jQuery for private methods and plugin encapsulation, override without touching global $, deprecated in 1.7 removed in 1.9, examples, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Call var copy = jQuery.sub() to get a private jQuery function.
Step 2Add or override methods on copy.fn (not on global jQuery.fn).
Step 3Use copy(selector) (or copy(this)) so chains see your private methods.

Return Value

Return ValueDescription
See examplesCheck the examples below for what this API returns in common cases.

jQuery Examples

Example 1 — Private Method on a Sub

Add a method that exists only on the copy — not on global jQuery.

jQuery
(function () {
  var sub$ = jQuery.sub();
  sub$.fn.myCustomMethod = function () {
    return "just for me";
  };
  console.log(sub$("body").myCustomMethod()); // "just for me"
})();

console.log(typeof jQuery("body").myCustomMethod); // undefined
Try It Yourself

How It Works

  1. Copy. jQuery.sub() returns a new jQuery-like function.
  2. Extend the copy. sub$.fn.myCustomMethod is private to that copy.
  3. Call via sub$. sub$("body") sees the private method → "just for me".

Example 2 — Override .remove() on the Copy

Trigger a custom event before calling the real jQuery.fn.remove.

jQuery
var myjQuery = jQuery.sub();

myjQuery.fn.remove = function () {
  this.trigger("remove");
  return jQuery.fn.remove.apply(this, arguments);
};

myjQuery(function ($) {
  $(document).on("remove", function (event) {
    console.log("removed:", event.target.id);
  });
  $("#item").remove(); // uses myjQuery.fn.remove
});
Try It Yourself

How It Works

  1. Fork. myjQuery is a copy; global $.fn.remove is unchanged.
  2. Wrap. The override fires a "remove" event, then calls the real remove.
  3. Scope. Only removals done through myjQuery get the extra event.

Example 3 — Plugin That Returns Sub Methods

Expose open / close only after calling .myplugin().

jQuery
var plugin = jQuery.sub();
plugin.fn.extend({
  open: function () { return this.show(); },
  close: function () { return this.hide(); }
});

jQuery.fn.myplugin = function () {
  this.addClass("plugin");
  return plugin(this); // chain sees open/close
};

$("#main").myplugin().open();
// $("#main").open() alone would fail — open is on the sub only
Try It Yourself

How It Works

  1. Private API. open / close live on plugin.fn, not global jQuery.fn.
  2. Entry point. .myplugin() is public and returns plugin(this).
  3. Chain. After .myplugin(), .open() works because the collection is the sub type.

Frequently Asked Questions

jQuery.sub() returned a new copy of the jQuery function whose properties and methods you could change without modifying the global jQuery / $ object. Private plugin methods and method overrides lived on that copy. Added in jQuery 1.5.
No. It was deprecated in jQuery 1.7 and removed in jQuery 1.9. Modern jQuery 3.x does not include $.sub(). Try-it labs on this page load jQuery 1.8.3 so you can still run authentic historical demos.
No. Official docs stress that sub() is not sandbox isolation. Events, .data(), and Ajax still go through the main jQuery. Only the jQuery function object and its fn methods are copied so you can extend or override them privately.
Two cases: (1) override methods like .remove() on a private copy without changing global jQuery, and (2) basic plugin encapsulation so helper methods exist only on the subbed instance. For new plugins, prefer the jQuery UI widget factory or plain modules.
noConflict() restores the previous global $ and can release the jQuery name — it does not create a fork of the API. sub() kept the same global jQuery and gave you a second, editable copy for private extensions.
Use ES modules or IIFEs for private helpers, jQuery.fn plugins that return the original collection, or the jQuery UI widget factory for stateful plugins. Do not rely on $.sub() in new code — it is gone from modern jQuery.

Next: trim() Method

Strip whitespace from the ends of a string with jQuery.trim().

trim() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful