Plugins
Beyond making many scripting tasks much easier, jQuery also makes itself easy to extend
with new functionality. This is done with an easy-to-understand Plugin API. Using jQuery’s
Plugin API, you can make your own chainable jQuery methods and even write entire complex
client-side applications completely as jQuery plugins.There are a lot of things you can do with plugins. Some of the more useful and prominent
examples of jQuery plugins are found in the jQuery UI library, which I discuss in more detail
in Chapter 12, “Draggable and Droppable.” Plugins in the jQuery UI library help you to imple-
ment functionality like drag-and-drop or selecting elements, and a variety of other function-
ality. There is also a thriving third-party development community for jQuery that produces
plugins for just about anything you can think of. You’ll examine a few third-party jQuery
plugins and even write one in Part II, “jQuery UI,” of this book. jQuery’s thriving plugin com-
munity exists largely thanks to how ridiculously easy it is to write plugins for jQuery.This chapter demonstrates how to use jQuery’s Plugin API and covers the basic concepts you
need to understand to start writing plugins of your own. Beyond what you learn about jQuery
plugin basics in this chapter, you also see more examples that use jQuery’s Plugin API later in
the book.Writing a Plugin
jQuery plugins are easy to implement. All you need to do is pass an object literal containing
the methods you want to extend jQuery with to the $.fn.extend() method.Writing a Simple jQuery Plugin
Example 9-1 demonstrates how to write a simple jQuery plugin. If you would like to try this
example for yourself, you can fi nd it in the Chapter 9 folder with the rest of the book’s exam-
ples that you can download from http://www.wrox.com/go/webdevwithjquery.<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>