Clear HTML Form Field on Entry


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

Clear a field on entry if the default text matches a supplied value. Generic to any field.


Copy this code and paste it in your HTML
  1. function activate_field(field,txt)
  2. {
  3. if (field.value === txt) {
  4. field.value = "";
  5. }
  6. else { $(field).select(); }
  7. }
  8.  
  9. /*
  10.  * call with an onfocus event
  11.  * example:
  12.  * <input type="text" onfocus="activate_field(this,\'City\')"
  13.  * id="city" name="city" value="City" />'
  14.  */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.