jQuery events; prevent "siblings" from triggering eachothers events -
using jquery 1.6.1, given have following html:
<div class="control"> <label>my control</label> <input type="text" /> <input type="text" /> </div>
when <input>
in <div class="control">
(hereafter control
) focused, <label>
(with position: relative;
) animates:
$('.control :input').bind('focus', function(e){ $(this).prevall('label').animate({ 'left': '-50px' }, 250); });
and when blurred, <label>
returns:
.bind('blur', function(e){ $(this).prevall('label').animate({ 'left': '0px' }, 250); });
however, if 1 of <input>
elements gains focus, , blurs focus switched <input>
within same control
(via tab or mouse click) events of course still fire, , <label>
animates , forth.
how can force blur event trigger when focus lost all inputs within given control
?
edit: updated answer based on more inputs provided op.
if slight delay of 1 second hide label fine can use settimeout/cleartimeout combination.. like:
<div class="control"> <label>my control</label> <input type="text" /> <input type="text" /> <input type="text" /> </div> <div class="control"> <label>my control</label> <input type="text" /> <input type="text" /> <input type="text" /> </div> <div class="control"> <label>my control</label> <input type="text" /> <input type="text" /> <input type="text" /> </div> <div class="control"> <label>my control</label> <input type="text" /> <input type="text" /> <input type="text" /> </div> <script type="text/javascript"> var timeoutids = []; $('.control').each(function(index, el){ $(':input', el).bind('focus', function(e){ cleartimeout(timeoutids[index]); $(this).prevall('label').animate({ 'left': '-50px' }); }); $(':input', el).bind('blur', function(e){ var = this; timeoutids[index] = settimeout(function(){ $(that).prevall('label').animate({ 'left': '0px' }); }, 500); }); }); </script>
working example: http://jsfiddle.net/tn9sv/2/
Comments
Post a Comment