form input prefill clearer and reset


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



Copy this code and paste it in your HTML
  1. // form input prefill clear on focus, and reset on blur (if empty)
  2.  
  3. function prefillClear(field) {
  4.  
  5. if (field.defaultValue==field.value) {field.value = '';}
  6.  
  7. else if (field.value == '') {field.value = field.defaultValue;}
  8.  
  9. }
  10.  
  11. // usage
  12.  
  13. <input type="text" name="input1" value="Enter your data" onfocus="prefillClear(this);" onblur="prefillClear(this);" />
  14.  
  15. // suggest using some further javascript to parse all input fields and apply these functions on domReady. This would save having to put the function calls inline.

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.