Posted By

accelm on 03/09/11


forms inputs values

Versions (?)

Clear all default field values for inputs

 / Published in: jQuery


If included in a global javascript, whenever an input receives focus it will clear the default value. If the element loses focus without any user input then it will put the default value back in there. Useful for when you aren't using <label> and putting the label in the value of the text field instead.

  1. $('input[type=text]').focus(function(){
  2. if (this.defaultValue==this.value){this.value = "";}
  3. });
  4. $('input[type=text]').blur(function(){
  5. if (this.value==""){this.value=this.defaultValue;}
  6. });

Report this snippet  

You need to login to post a comment.