This works, but there are a couple of things we need to do for our plugin to survive in the real world. One of jQuery's features is chaining, when you link five or six actions onto one selector. This is accomplished by having all jQuery object methods return the original jQuery object again (there are a few exceptions: .width()
called without parameters returns the width of the selected element, and is not chainable). Making our plugin method chainable takes one line of code:
$.fn.greenify = function() {
this.css( "color", "green" );
// return the reference for chaining
return this;
}
$( "a" ).greenify().addClass( "greenified" );