jQuery swap form value plugin.
June 8th, 2010
Here’s a simple, lightweight plugin to swap default values in a form field. It works with both textarea and input elements, and runs solidly across browsers. For example:
How to make it work.
Just add a swap_value attribute to your element specifying the value you’d like shown in the field:
<input type="text" name="username" swap_value="username" />
And call the plugin like this:
$("input[swap_value], textarea[swap_value]").swap_value();
You can also change the default text colors by passing arguments to the function:
$("input[swap_value], textarea[swap_value]").swap_value({
normal_color: '#3E474D',
default_color: '#BCBCBD'
});
Don’t forget about accessibility.
For accessibility reasons, you should always include labels for each of your form elements. If you don’t want labels cluttering up your view, I suggest you hide them:
Some more powerful alternatives.
There are some similar, although much more powerful, plugins available for swapping form values. If you’re looking something more fully-featured, I suggest you checkout jquery watermark. Also the in-field labels plugin seems pretty awesome.
Of course, the right choice depends on the needs of your application. Better to go with something simple, if that’s all you need.