Skip to content

Instantly share code, notes, and snippets.

@vjt
Created June 17, 2009 11:26
Show Gist options
  • Save vjt/131194 to your computer and use it in GitHub Desktop.
Save vjt/131194 to your computer and use it in GitHub Desktop.
// Just what the name says: hide the default value of a text input onFocus, and restores it if empty onBlur.
// Simple and effective, jQuery is awesome! Sample markup:
//
// <input id="search" type="text" value="Search" />
// <script type="text/javascript">$('#search').toggleDefaultValue()</script>
//
// - [email protected]
//
$.fn.toggleDefaultValue = function() {
return this.each(function() {
var self = $(this);
var defaultValue = self.attr('value');
self.focus(function() {
if(self.attr('value') == defaultValue) {
self.attr('value', '');
}
}).blur(function() {
if(self.attr('value') == '') {
self.attr('value', defaultValue);
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment