Monday, December 22, 2008

How to add your own custom functions to jQuery

Recently I wrote a template on how to extend jQuery and how to create your own functions. Since then I came across even smaller code snippet on how to add your own custom functions, in other words how to create plugins for jQuery. So without further ado:

$.fn.myFunction = function() { 
    return $(this).addClass('changed'); 
}

And now, use it like this:

$('.changePlease').myFunction();

And that is all there is :)


7 comments:

ABitSmart said...

Thanks a lot!!
:)

Gabriel Porras said...

What if I need a parametrized function?
Thanks!

Antoine said...

Nice

Uzbekjon said...

@Gabriel Porras, you can simply do this:

$.fn.myFunction = function(MYVAR) {
var args = MYVAR;
...
}

@ABitSmart, no probs. @Antoine thanks.

avdhut said...

good article thanks

Anonymous said...

very helpful :)

Anonymous said...

So simple, yet so powerful!!!

Post a Comment