How to perform action when element gains focus


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



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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.