Posted By

omerta83 on 07/08/10


Tagged

javascript jquery


Versions (?)

How to perform action when element gains focus


 / Published in: JavaScript
 

  1. jQuery('form')
  2. .focusin(function(){
  3. jQuery(this).addClass('focused');
  4. });
  5. .focusout(function(){
  6. jQuery(this).removeClass('focused');
  7. });
  8.  
  9. //However, the preferred way to do this is using
  10. // .focus() and .blur()
  11.  
  12. //For when an element loses it's focus use .blur()
  13. $('#target').blur(function() {
  14. alert('Handler for .blur() called.');
  15. });
  16.  
  17. //For when an element gains focus use .focus()
  18. $('#target').focus(function() {
  19. alert('Handler for .focus() called.');
  20. });
  21.  

Report this snippet  

You need to login to post a comment.