Can anyone tell me the difference between blur
and focusout
, focus
and focusin
with a simple example?
The main difference between this event and blur is that focusout bubbles while blur does not. The opposite of focusout is focusin .
The focusin event fires when an element is about to receive focus. The main difference between this event and focus is that focusin bubbles while focus does not. The opposite of focusin is focusout .
The focusout event occurs when an element (or any elements inside it) loses focus. The focusout() method attaches a function to run when a focusout event occurs on the element, or any elements inside it. Unlike the blur() method, the focusout() method also triggers if any child elements lose focus.
Definition and UsageThe blur event occurs when an element loses focus. The blur() method triggers the blur event, or attaches a function to run when a blur event occurs. Tip: This method is often used together with the focus() method.
The focusin
and focusout
events bubble, the focus
and blur
events doesn't. That means that you can use the focusin
and focusout
on the parent element of a form field.
Demo: http://jsfiddle.net/pAp4E/
HTML:
<div class="parent">
<input type="text" />
</div>
<div class="log"></div>
Javascript:
$('.parent')
.focusin(function(){log('div focusin');})
.focusout(function(){log('div focusout');})
.focus(function(){log('div focus');})
.blur(function(){log('div blur');});
$('input')
.focusin(function(){log('input focusin');})
.focusout(function(){log('input focusout');})
.focus(function(){log('input focus');})
.blur(function(){log('input blur');});
function log(str){
$('.log').append($('<div/>').text(str));
}
When you run it, you see that only the input gets all the events, the parent only gets the focusin
and focusout
events.
The focus and blur events keep track of the elements the user focuses on.
focus
Fires when a focusable element gains the focus
blur
Fires when a focusable element loses the focus
focusin and focusout
Fire at the same time as focus and blur, but bubble.
for example check this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With