Chapter 5. Collection plugins
This chapter covers
- Defining collection plugins
- Using a plugin framework
- Applying the design principles
- Creating a complete collection plugin
Now that you’ve read the theory behind designing and implementing a plugin, you’ll see how it’s implemented in practice. To make things more concrete, you’ll create a relatively simple plugin that provides a useful service, while still being complex enough to show most of the techniques involved in any plugin.
The plugin you’ll build complements existing functionality provided by the browser. Normal text input fields have a maxlength attribute that allows you to limit how much text may be entered into a field. Such a restriction helps enforce limits that may be imposed by databases and other storage mechanisms. But the multiline textarea field has no such attribute and allows unlimited text entry. To address this situation, you can create a collection plugin to control the amount of acceptable text, and provide valuable feedback along the way.
As you may recall from chapter 3, jQuery usually operates under a select and act pattern. You find the elements of interest, either directly by using selectors and filters, or by traversing the DOM from an existing selection, and then apply some functionality to them. For example, to hide all paragraphs with a class of note, you’d use the following code: