Is it appropriate to create jQuery plugins which are specific to a single page? I’ve been creating generic plugins that are not tied to any context and contain no business logic, but some people I’ve talked to suggest that almost all javascript, including business logic and logic specific to a single page, should be inside of jQuery plugins.
Is it appropriate to have a validateformXYZ plugin which validates a specific HTML form?
I’m buying into jQuery 100% but am not sure if this is a misuse or not.
I tend to use a different approach to this. When I have objects (forms, usually) that need to have a lot of client-side behavior bound to them, I define JavaScript objects that represent those forms.
For example, a contact form might have a corresponding Contact object that gets initialized with the context of the inner-form elements (textboxes, buttons, etc.) During initialization you can then use jQuery (or your framework of choice) to add behaviors to these elements. In your case you’d bind a click event which would call a validation function in your Contact object.
This seems cleaner than creating a whole bunch of unrelated plugins to provide very specific behavior.
EDIT TO PROVIDE EXAMPLE
Here’s a quick & simple sample of this in action: http://jsbin.com/elefi/edit
The code:
Note that IE6 seems to dislike jsbin.com, so open it in FF