How to delegate a focal event?

Possible duplicate:
Focusing and blurring jQuery events don't bubble

var default_input_value = null; jQuery('body').on('focus focusout', 'input[type="text"]', function(event){ if(event.type === 'focus'){ default_input_value = jQuery(this).val(); jQuery(this).val(''); }else if(event.type === 'focusout') if(jQuery(this).val().length === 0) jQuery(this).val(default_input_value); } ); 

This code simply does not respond to the event. The postscript is important for input[type="text"] , because jQuery focus also works in checkbox in some situations ....

+6
source share
1 answer

There is a description in the .on() jquery document:

The focus and blur events are set by the W3C so as not to bubble, but jQuery defines events with multiple focusin and focusout that bubble. When focus and blur are used to attach delegated event handlers, jQuery matches the names and passes them as focusin and focusout respectively. For consistency and clarity, use the bubble event type names.

Hope this would be helpful.

And you can try this code:

 var default_input_value = null; jQuery('body').on('focusin focusout', 'input[type="text"]', function(event){ if(event.type === 'focusin'){ default_input_value = jQuery(this).val(); jQuery(this).val(''); }else if(event.type === 'focusout'){ if(jQuery(this).val().length === 0) jQuery(this).val(default_input_value); } }); 
+18
source

All Articles