We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

gbot on 03/09/08


Tagged

forms prefill


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

gbot
SpinZ


form input prefill clearer and reset


Published in: JavaScript 


  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 

You need to login to post a comment.