Clear form field on focus

Hello friends. To clear a field onfocus use this simple javascript function. This is very helpful to give some default value to the field. Like search boxes contain some text for hint and removed automatically when we focus on the search box.

The javascript
 
  <script type="text/javascript">
     function clearField(field){
       if (field.defaultValue == field.value){
           field.value = '';
         }else if (field.value == '') {
           field.value = field.defaultValue;
         }
       }
  </script>


The html

  <input type="text" onFocus="clearField(this)" 
       onBlur="clearField(this)" value="Default value" />


The example

Clear form field on focus Clear form field on focus Reviewed by JS Pixels on June 20, 2011 Rating: 5

1 comment:

Altaf Web. Powered by Blogger.