/ Published in: jQuery

Originally from http://www.electrictoolbox.com (see src)\r\nClear the default value of a form field when you click on it (when you want to type) and put it back if you leave without typing anything.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//http://www.electrictoolbox.com/jquery-change-default-value-on-focus/ $(document).ready(function() { $('.default-value').each(function() { var default_value = this.value; $(this).css('color', '#666'); // this could be in the style sheet instead $(this).focus(function() { if(this.value == default_value) { this.value = ''; $(this).css('color', '#333'); } }); $(this).blur(function() { if(this.value == '') { $(this).css('color', '#666'); this.value = default_value; } }); }); }); --- <input type="text" class="default-value" size="30" value="Enter keywords here" /> <input type="text" class="default-value" size="30" value="Another search box" />
URL: http://www.electrictoolbox.com/jquery-change-default-value-on-focus/
Comments
