Posted By


markrocky on 12/06/07

Tagged


Statistics


Viewed 89 times
Favorited by 0 user(s)

enableClearDefault();


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

attaches an event (using addEvent) to all text fields that clears the contents when selected


Copy this code and paste it in your HTML
  1. function enableClearDefault() {
  2. if (!document.getElementsByTagName) return;
  3. var inputFields = document.getElementsByTagName("input");
  4. for (var i=0; i<inputFields.length; i++) {
  5. var inputField = inputFields[i];
  6. if (inputField.type=="text") {
  7. addEvent(inputFields[i], 'focus',
  8. function() {
  9. clearDefault(this);
  10. }
  11. );
  12. }
  13. }
  14. };
  15. function clearDefault(el) {
  16. if ((el.defaultValue==el.value) && (isNaN(el.value))) {
  17. el.select();
  18. }
  19. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.