Created
June 17, 2009 11:26
-
-
Save vjt/131194 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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