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

noah on 04/27/07


Tagged

forms pattern web usability ux oo ixd quick-n-dirty


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

inamorix
dkhawk


clear form field on first focus (inline version)


Published in: JavaScript 


The unobtrusive version requires an onload statement. Doing that in a backward-compatible fashion can be a pain in the ass. So here is an inline version for use with older code. This should work "out of the box" with any browser that has javascript.

  1. <script type="text/javascript">
  2. var Handler = {
  3. clearedOnce : false,
  4. clear : function ( field ) {
  5. if (this.clearedOnce == false) {
  6. field.value = '';
  7. this.clearedOnce = true;
  8. }
  9. }
  10. }
  11. </script>
  12. <!-- then in the XHTML: -->
  13. <input onfocus="Handler.clear(this)" value="foobar" />

Report this snippet 

You need to login to post a comment.