Blankman
Blankman

Reputation: 266978

extending jquery on the main element object

I've seen some jquery code where people extend the main object like:

 $('someId').myCustomObject();

Is this possible or am I mistaken? (if yes, how?)

Upvotes: 1

Views: 333

Answers (2)

stusmith
stusmith

Reputation: 14103

Yes it's easily possible. The standard pattern for building extensions is:

(function($) {

  $.fn.myCustomObject = function(options) {
    var defaults = { ... };
    var opts = $.extend(defaults, options);

    this.each(function(i) {

      ... // Act on each item, $(this).
      ... // Use opts.blah to read merged options.

    });
  };

})(jQuery);

This allows you to use '$' in the plug-in, yet allows compatibility mode.

Upvotes: 3

Rich
Rich

Reputation: 36806

I believe what you're looking for is jQuery.fn.extend:

jQuery.fn.extend

Upvotes: 0

Related Questions