jQuery sub() Method

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is jQuery.sub()?

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).

Remember
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
NeedUse (today)
Private helpersIIFE / ES modules
Stateful pluginsjQuery UI widget factory
Release global $jQuery.noConflict()

How to Use jQuery.sub()

Historical pattern only — requires jQuery 1.5–1.8.

  1. Call var copy = jQuery.sub() to get a private jQuery function.
  2. Add or override methods on copy.fn (not on global jQuery.fn).
  3. Use copy(selector) (or copy(this)) so chains see your private methods.
  4. Remember: events, data, and Ajax still run through the main jQuery.
PieceRequired?Role
ArgumentsNoneNo parameters
Return—A new jQuery constructor/function copy
Versions—1.5–1.8 only (gone in 1.9+)

Pseudocode

Pseudocode
function sub():
    copy = clone of jQuery function + jQuery.fn
    // copy.fn can be extended without changing global jQuery.fn
    return copy

// Use cases (official):
// 1) Private methods only on the copy
// 2) Override methods (e.g. .remove) on the copy
// 3) Plugin returns plugin(this) so chain sees plugin methods

// Not isolation: events / data / Ajax → main jQuery

Cheat Sheet

Keep these facts handy when reading legacy plugin code.

TopicDetail
SyntaxjQuery.sub()
AddedjQuery 1.5
DeprecatedjQuery 1.7
RemovedjQuery 1.9
Isolation?No — not a sandbox
vs noConflictForks API vs releases global name
TodayModules / widget factory

Live Preview

Runs on jQuery 1.8.3 (still has $.sub). Pick a demo action from the official use cases.

Legacy browser demo of jQuery.sub() — removed in jQuery 1.9+.

Result
var sub$ = jQuery.sub();
sub$.fn.myCustomMethod = …
sub$("body").myCustomMethod()
→ "just for me"

jQuery Examples

Official patterns from the API docs. Labs use jQuery 1.8.3 (except example 5, which proves removal on 3.x).

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".
  4. Global untouched. jQuery("body").myCustomMethod is still undefined.

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.
  4. Lesson. Regular $("#item").remove() on global jQuery would not trigger this hook.

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.
  4. Direct call fails. $("#main").open() without myplugin() has no such method.

Example 4 — Global $ Stays Clean

Prove a method on sub$ never lands on jQuery.fn.

jQuery
var sub$ = jQuery.sub();
sub$.fn.secret = function () { return 42; };

console.log(typeof sub$.fn.secret);   // "function"
console.log(sub$("body").secret());   // 42
console.log(typeof jQuery.fn.secret); // "undefined"
Try It Yourself

How It Works

  1. Attach privately. secret is added only to sub$.fn.
  2. Works on the copy. sub$("body").secret() returns 42.
  3. Global check. jQuery.fn.secret remains undefined.
  4. Why it mattered. Plugins could hide helpers without polluting every $() call site.

Example 5 — Missing in Modern jQuery

Confirm typeof jQuery.sub on jQuery 3.7.1.

jQuery
// Load jQuery 3.7.1
console.log(jQuery.fn.jquery);       // "3.7.1"
console.log(typeof jQuery.sub);      // "undefined"

// Prefer modules, IIFEs, or the widget factory instead
Try It Yourself

How It Works

  1. Modern build. jQuery 3.x no longer ships $.sub.
  2. Check. typeof jQuery.sub is "undefined".
  3. Legacy only. Reading old code? Pin jQuery ≤1.8 or rewrite the plugin.
  4. Replacement. Use modules or the widget factory for new work.

Edge Cases & Pitfalls

Check these before assuming $.sub() still exists or isolates anything.

removed

Gone since jQuery 1.9

typeof $.sub === "undefined" on modern builds. Calling it throws. Do not ship new code that depends on it.

sandbox

Not a sandbox

Events, Ajax, and .data() still go through main jQuery. Official docs warn: isolation was never the goal.

chain

Private methods need the sub collection

After leaving a subbed chain (or starting from global $), plugin-only methods disappear. Return plugin(this) when you need them to stay.

alt

Prefer widget factory / modules today

Official guidance: for plugin development, strongly consider the jQuery UI widget factory (state + sub-methods) or plain modules.

Browser Support

jQuery.sub() needed a browser DOM and jQuery 1.5–1.8. It was removed in jQuery 1.9 and is absent from jQuery 3.x — not available in Node.js without a legacy jQuery build + DOM.

✓ Removed 1.9 · Legacy only

jQuery.sub()

Historical API: browsers with jQuery 1.5–1.8. Modern jQuery builds do not include $.sub(). Use modules or the widget factory instead.

Legacy jQuery 1.5–1.8 only
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
$.sub() Removed

Bottom line: Do not use $.sub() in new projects. Read this page to understand legacy plugins; rewrite with modules or the widget factory.

Performance Notes

ApproachCostBest for
jQuery.sub()Copy overheadLegacy private API forks (historical)
IIFE / module helpersNegligiblePrivate functions today
Widget factoryModestStateful plugins with methods

Performance is not the reason sub() was removed — maintainability and clearer plugin patterns were. Prefer modules over copying the whole jQuery API.

Key Takeaways

  • Role: $.sub() forked jQuery so you could extend a private copy.
  • Status: deprecated 1.7, removed 1.9 — absent in jQuery 3.
  • Not a sandbox: events / Ajax / data still use main jQuery.
  • Today: modules or the widget factory — not $.sub().

One line: jQuery.sub() made a private editable copy of jQuery for plugins — useful historically, removed in 1.9, and never a true sandbox.

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()

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

trim() tutorial →

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