Clear Text Field of its default value, and return the original value


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



Copy this code and paste it in your HTML
  1. // A simple, lightweight, on-the-fly script to handle the clearing and resetting of a text field
  2. var searchField = document.getElementById('search');
  3. var defaultText = searchField.value;
  4. searchField.onfocus = function(){
  5. if( searchField.value == defaultText ){
  6. searchField.value = '';
  7. }
  8. };
  9. searchField.onblur = function(){
  10. if( searchField.value == '' ){
  11. searchField.value = defaultText;
  12. }
  13. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.