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


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

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.


Copy this code and paste it in your HTML
  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. });

URL: http://darrenhuskie.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.