input focus&blur actions


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

input focus&blur actions


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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.