views:

25

answers:

1

I need to perform a function when a user comes out of focus. So far I can do it when they go into focus but how do I do it the other way round:

 $("input.default-value").focus(function() {
                 $(this).css("color", active_color);        
                                }); 
+3  A: 

You're looking for .blur()

$("input.default-value").focus(function() {
       $(this).css("color", active_color);        
 }).blur(function() {
       $(this).css("color", inactive_color);     
}); 

Of course, i'd recommend using classes to add the css.

.default-value.active { 
   color: #FF0000;
} 

$("input.default-value").focus(function() {
           $(this).addClass('active');        
     }).blur(function() {
           $(this).removeClass('active'); 
    }); 

but i digress..

(and if you don't care about ie7 and under, you can use the :focus pseudo-class)

Dan Heberden