Posted By

noah on 02/24/07


Tagged

clear default input forms Value field onfocus usability ux ixd


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

vali29


clear form field on first focus


 / Published in: JavaScript
 

  1. <html>
  2. <head>
  3. <title>Clear form field on initial focus</title>
  4. <script type="text/javascript">
  5. function clearOnInitialFocus ( fieldName ) {
  6. var clearedOnce = false;
  7. document.getElementById( fieldName ).onfocus = (function () {
  8. if (clearedOnce == false) {
  9. this.value = '';
  10. clearedOnce = true;
  11. }
  12. })
  13. }
  14. window.onload = function() { clearOnInitialFocus('myfield');
  15. </script>
  16. </head>
  17. <body>
  18. <h1>Clear form field on initial focus</h1>
  19. <input name="myfield" id="myfield" value="default message">
  20. </body>
  21. </html>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: noah on February 24, 2007

This version depends on Prototype.js.

Posted By: noah on April 30, 2007

This no longer depends on Prototype. Why I'm typing comments on my own code, instead of just editing the description, I don't know.

You need to login to post a comment.