Chapter 9. Plugins

Beyond making many scripting tasks much easier, jQuery also makes itself very easy to extend with new functionality. This is done with a very 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 lots 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 begin discussion of in Chapter 10. Plugins in the jQuery UI library help you to implement functionality like drag-and-drop or selecting elements, and a variety of other functionality. There is also a thriving third-party development community for jQuery that produces plugins for just about anything you can think of. jQuery's thriving plugin community exists largely thanks to how ridiculously easy it is to write plugins for jQuery.

In this chapter, I demonstrate how to use jQuery's Plugin API and cover the basic concepts you'll need to understand to start writing plugins of your own. Beyond what you learn about jQuery plugin basics in this chapter, you'll also see more examples that use jQuery's Plugin API later in the book.

Writing a Plugin

jQuery plugins are very 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. The following code demonstrates how this is done:

<!DOCTYPE html PUBLIC "-//W3C//DTD ...

Get Beginning JavaScript® and CSS Development with jQuery now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.