Default value for input fields (jQuery version)


/ Published in: jQuery
Save to your folder(s)

See also the [plain JavaScript version](http://snipplr.com/view/41717/default-value-for-input-fields/).


Copy this code and paste it in your HTML
  1. var elem = $(':text');
  2.  
  3. // delete default value on focus
  4. elem.focus(function()
  5. {
  6. if ( this.value == this.defaultValue )
  7. {
  8. this.value = '';
  9. }
  10. });
  11.  
  12. // regain default value on blur
  13. elem.blur(function()
  14. {
  15. if ( this.value == '' )
  16. {
  17. this.value = this.defaultValue;
  18. }
  19. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.