jQuery Plugin Actions vs Utilities
Jonathan Snook posted about developing a jQuery plugin. I started a comment and it turned into a post.
$.formatDate(new Date(), ‘m d Y’)
This would return a string, not an object for chaining. So now if we want to perform an action with it… such as insert the value into a div we would use:
$(‘#myDiv’).html($.formatDate(new Date(), ‘m d Y’));
I just wanted to point out a common pitfall when making a plugin. Some are utilities, and some are actions. A utility plugin returns a result, but an action plugin is something that returns an object for chaining. Take the method attachDatepicker, for instance:
$(‘#myInput’).datepicker().val(‘select a date’);
It can be chained because it returns the input with the datepicker attached. You can see more examples of actions vs utilities in the Datepicker Documentation or look at the jQuery Utilities vs jQuery Manipulation.