tags:

views:

90

answers:

5

The easiest way?

So that I can call something like:

$('#id').applyMyOwnFunc
+5  A: 

Please see Defining your own functions in jQuery:

In this post i want to present how easy define your own functions in jQuery and using them.

keyboardP
When that link dies, this answer is really going to be useless.
tvanfosson
+1  A: 

jQuery have the extend thingy to do that

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

you can see the documentation there

RageZ
+3  A: 

The jquery documentation has a section on plugin authoring, where I found this example:

jQuery.fn.debug = function() {
  return this.each(function(){
    alert(this);
  });
};

Then you'd be able to call it this way:

$("div p").debug();
Marcel Levy
+1  A: 

This is the pattern that I prefer to define my own plugins.

(function($) {

    $.fn.extend({
        myfunc: function(options) {
            options = $.extend( {}, $.MyFunc.defaults, options );

            this.each(function() {
                new $.MyFunc(this,options);
            });
            return this;
        }
    });

    // ctl is the element, options is the set of defaults + user options
    $.MyFunc = function( ctl, options ) {
         ...your function.
    };

    // option defaults
    $.MyFunc.defaults = {
        ...hash of default settings...
    };

})(jQuery);

Applied as:

$('selector').myfunc( { option: value } );
tvanfosson
A: 

Here's a plugin, in it's simplest form...

jQuery.fn.myPlugin = function() {
  // do something here
};

You'll really want to consult the documentation though:

http://docs.jquery.com/Plugins/Authoring

JGarrido