Posted By

noah on 04/27/07


Tagged

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


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

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.