Posted By

mortu on 08/12/11


Tagged

input jquery focus Blur


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

spee66


input focus&blur actions


 / Published in: jQuery
 

input focus&blur actions

  1. //search .click events
  2. var default_value = new Array();
  3. $('input[type=text], textarea').each(function(){
  4. default_value[$(this).attr('name')] = this.value;
  5. });
  6.  
  7. $('input[type=text], textarea').focus(function(){
  8. if(typeof(default_value[$(this).attr('name')]) != 'undefined' && this.value == default_value[$(this).attr('name')]){
  9. this.value = '';
  10. }
  11. });
  12. $('input[type=text], textarea').blur(function(){
  13. if(typeof(default_value[$(this).attr('name')]) != 'undefined' && this.value == ''){
  14. this.value = default_value[$(this).attr('name')];
  15. }
  16. });
  17.  
  18.  
  19.  
  20. // even a better option - this takes the title value
  21.  
  22. // toggle input text on blur
  23. $('input.toggle-text').focus(function(){
  24. var input = $(this);
  25. var defaultText = input.attr('title');
  26. input.val('');
  27. input.blur(function() {
  28. var userInput = $(this).val();
  29. if (userInput == ''){
  30. $(this).val(defaultText);
  31. }
  32. });
  33. });

Report this snippet  

You need to login to post a comment.