Here is an example jQuery plugin I put together to help give a "Write Your First jQuery Plugin" presentation to some co-workers. The plugin itself is rather trivial -- it takes a ul and cycles through the li's it contains. I've found the example to be a good reference, so I figured I would post it here so I can't loose it (and of course to share with others).
The project is actually split into 9 different examples that build on each other:
- Common example of functionality written inline on the page.
- Basic jQuery method.
- Passing in options.
- Using the $ symbol in the plugin.
- Private functions.
- Static functions.
- Public functions / using $.data() to hold state.
- Namespacing events.
- Adding style with CSS.
If you are looking for a full-blown plugin example, look at example 9.
I plan on cleaning up the code and adding a bunch of comments at some point.
I put this together from a number of different sources, these two are definitely worth checking out:
- http://docs.jquery.com/Plugins/Authoring -- Super helpful guide to authoring plugins.
- jQuery Cookbook -- Great book with contributions from a number of jQuery experts that was put together by Codey Lindley. Contains a helpful chapter on creating plugins that was contributed by Mike Hostetler.