Here is a solution for text / password / textarea (I'm not sure that I forgot others that can get focus, but they could be easily added by changing the if clauses ... in the design it could be improved by putting the if body in its own function, to identify suitable inputs that can get focus).
Assuming you can rely on a user playing in a browser that is not prehistoric (http://www.caniuse.com/#feat=dataset):
<script> //The selector to get the text/password/textarea input that has focus is: jQuery('[data-selected=true]') jQuery(document).ready(function() { jQuery('body').bind({'focusin': function(Event){ var Target = jQuery(Event.target); if(Target.is(':text')||Target.is(':password')||Target.is('textarea')) { Target.attr('data-selected', 'true'); } }, 'focusout': function(Event){ var Target = jQuery(Event.target); if(Target.is(':text')||Target.is(':password')||Target.is('textarea')) { Target.attr('data-selected', 'false'); } }}); }); </script>
For prehistoric browsers you can use the uglier one:
<script> //The selector to get the text/password/textarea input that has focus is: jQuery('[name='+jQuery('body').data('Selected_input')+']') jQuery(document).ready(function() { jQuery('body').bind({'focusin': function(Event){ var Target = jQuery(Event.target); if(Target.is(':text')||Target.is(':password')||target.is('textarea')) { jQuery('body').data('Selected_input', Target.attr('name')); } }, 'focusout': function(Event){ var Target = jQuery(Event.target); if(Target.is(':text')||Target.is(':password')||target.is('textarea')) { jQuery('body').data('Selected_input', null); } }}); }); </script>
Magnitus Jul 31 '12 at 18:46 2012-07-31 18:46
source share