Skip to content

Instantly share code, notes, and snippets.

@pacoguzman
Created December 10, 2010 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pacoguzman/735932 to your computer and use it in GitHub Desktop.
Save pacoguzman/735932 to your computer and use it in GitHub Desktop.
Clear Default for input text fields using prototype 1.7.0
/*
* Clear Default Text: functions for clearing and replacing default text in
* <input> elements.
*
* by Ross Shannon, http://www.yourhtmlsource.com/
* Migrated to prototype 1.7.0 by @fjguzman
*/
document.observe("dom:loaded", function() {
function clearDefaultText(e) {
if (this.getValue() == this.retrieve('defaultText'))
this.writeAttribute('value', "")
}
function replaceDefaultText(e) {
if (this.getValue() == '' && this.retrieve('defaultText'))
this.writeAttribute('value', this.retrieve('defaultText'))
}
$$("input[type='text']").each(function(input, index) {
if(!input.hasClassName('cleardefault'))
return;
input.observe('focus', clearDefaultText).observe('blur', replaceDefaultText);
/* Save the current value */
if (input.getValue() != '')
input.store('defaultText', input.getValue())
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment