jQuery .fn.extend() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Core

Definition

jQuery.fn.extend( object ) (alias $.fn.extend) merges the methods of an object onto jQuery.fn — the prototype shared by every jQuery collection — so each new method works on any $("selector") result (added in jQuery 1.0). It is the classic way to write a jQuery plugin.

Syntax

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

jQuery
jQuery.fn.extend({
  methodName: function () {
    return this;   // keep the chain alive
  }
});

$("p").methodName();           // use it on any collection

// Same idea for a single method:
$.fn.methodName = function () { /* ... */ };
CallWhat it does
$.fn.extend(object)Merge every property of object onto jQuery.fn as new instance methods.
$.fn.name = functionAdd one method by hand — equivalent to extending with a single key.
$.extend(object)Merge onto the jQuery function itself ($.name()), not onto collections.
$.fn === $.prototypetrue — fn is just a short alias for the prototype.

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

Parameters

ParameterDescription
objectObject — an object whose methods are merged onto jQuery.fn. Inside each method, this is the jQuery collection the method was called on.
this (inside a method)The jQuery object — call other jQuery methods on it (this.css(), this.each()) and return it so callers can keep chaining.

Return Value

Return ValueDescription
ObjectThe jQuery.fn object itself, so you can keep extending it. You rarely need this value.
Your plugin methodNormally returns this (the jQuery object) for chaining. A getter-style plugin may return a plain value instead.

jQuery Examples

Example 1 — Official Demo: check() and uncheck()

The jQuery API sample adds two methods that tick or clear every matched checkbox.

jQuery
jQuery.fn.extend({
  check: function () {
    return this.each(function () { this.checked = true; });
  },
  uncheck: function () {
    return this.each(function () { this.checked = false; });
  }
});

// Use the newly created methods
$("input[type='checkbox']").check();
console.log($("#a").prop("checked"));   // true

$("input[type='checkbox']").uncheck();
console.log($("#a").prop("checked"));   // false
Try It Yourself

How It Works

  1. Extend. One object with two functions is merged onto jQuery.fn.
  2. Loop. this.each() visits every checkbox in the collection.
  3. Chain. return this.each(...) hands the collection back to the caller.

Example 2 — Plugin with Default Options

Wrap the plugin in an IIFE and merge user options over defaults with $.extend().

jQuery
(function ($) {
  var defaults = { color: "#fff3a3", border: "1px solid #e0c200" };

  $.fn.extend({
    highlight: function (options) {
      var settings = $.extend({}, defaults, options);
      return this.css({
        backgroundColor: settings.color,
        border: settings.border
      });
    }
  });
})(jQuery);

$("#one").highlight();
$("#two").highlight({ color: "#c8f7c5" });

console.log($("#one").css("background-color"));  // "rgb(255, 243, 163)"
console.log($("#two").css("background-color"));  // "rgb(200, 247, 197)"
Try It Yourself

How It Works

  1. IIFE. (function ($) { ... })(jQuery) keeps $ safe even with noConflict().
  2. Merge options. $.extend({}, defaults, options) builds the settings without touching defaults.
  3. Apply and return. this.css(...) styles the elements and returns the collection.

Example 3 — $.fn.extend() vs $.extend()

Same function, different target: one adds collection methods, the other adds utilities on $.

jQuery
$.extend({
  sayHi: function (name) { return "Hi, " + name; }   // $.sayHi()
});

$.fn.extend({
  shout: function () {                               // $("p").shout()
    return this.text(this.text().toUpperCase());
  }
});

console.log(typeof $.sayHi);      // "function"
console.log(typeof $.fn.sayHi);   // "undefined"
console.log(typeof $.shout);      // "undefined"
console.log(typeof $.fn.shout);   // "function"

$("#msg").shout();
console.log($("#msg").text());    // "HELLO"
Try It Yourself

How It Works

  1. Static. $.extend(obj) adds sayHi to the jQuery function.
  2. Instance. $.fn.extend(obj) adds shout to every collection.
  3. Call it. $.sayHi() needs no selection; $("#msg").shout() works on elements.

Frequently Asked Questions

jQuery.fn.extend(object) copies the properties of object onto jQuery.fn (jQuery.prototype). Every function you add becomes an instance method, so $("selector").yourMethod() works on any jQuery collection. It is the standard way to write a jQuery plugin.
They are the same function with a different this. $.extend(obj) with one argument merges onto the jQuery function itself, giving utilities like $.sayHi(). $.fn.extend(obj) merges onto jQuery.fn, giving instance methods like $("p").shout(). Use $.extend() with several objects to merge plain objects, for example plugin options.
jQuery methods are chainable because they return the jQuery object. If your plugin returns nothing, code like $("p").myPlugin().addClass("on") fails with a TypeError. Return this (or this.each(...)) unless your plugin is a getter that must return a value.
Not if you need the collection. Arrow functions do not have their own this, so this would not point at the jQuery object. Use a regular function expression inside jQuery.fn.extend().
Yes for a single method. jQuery.fn.extend is simply convenient when you add several methods at once. Both end up as properties on jQuery.fn.
Wrap it in an IIFE that receives jQuery as $: (function ($) { $.fn.extend({ ... }); })(jQuery). Inside, $ always means jQuery, even if another library owns the global $.

Next: .ready() Method

Learn how to run code when the DOM is ready with .ready().

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

9 people found this page helpful