The easiest way?
So that I can call something like:
$('#id').applyMyOwnFunc
The easiest way?
So that I can call something like:
$('#id').applyMyOwnFunc
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.
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();
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 } );
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: