O'Reilly logo

Beginning JavaScript® and CSS Development with jQuery by Richard York

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required