Posted By

Huskie on 04/10/12


Tagged

clear jquery text function fields focus single Blur statement


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

BrockSamsom


Clear all text fields on focus based on original value with single function


 / Published in: jQuery
 

URL: http://darrenhuskie.com

Clear all text fields on focus based on original value with single function. The code checks for a 'title' attribute on each input with class 'text' and compares this to the current value of the field.

  1. var $originalValue = '';
  2.  
  3. $('.text').focus(function () {
  4. $(this).addClass('active');
  5. if ($(this).val() == $(this).attr('title')) {
  6. $originalValue = $(this).val();
  7. $(this).val('');
  8. }
  9. });
  10.  
  11. $('.text').blur(function () {
  12. $(this).removeClass('active');
  13. if ($(this).val() == '') {
  14. $(this).val($originalValue);
  15. }
  16. });

Report this snippet  

You need to login to post a comment.