Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between focusin/focusout and focus/blur, with example

Can anyone tell me the difference between blur and focusout, focus and focusin with a simple example?

like image 936
Imran Qadir Baksh - Baloch Avatar asked Oct 22 '11 10:10

Imran Qadir Baksh - Baloch


People also ask

What is the difference between Blur and Focusout?

The main difference between this event and blur is that focusout bubbles while blur does not. The opposite of focusout is focusin .

What is the difference between focus and 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 .

What is Focusout event?

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.

What is blur function in Javascript?

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.


2 Answers

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.

like image 181
Guffa Avatar answered Sep 23 '22 06:09

Guffa


The focus and blur events keep track of the elements the user focuses on.

  1. focus

    Fires when a focusable element gains the focus

  2. blur

    Fires when a focusable element loses the focus

  3. focusin and focusout

    Fire at the same time as focus and blur, but bubble.

for example check this

like image 29
Rupesh Pawar Avatar answered Sep 22 '22 06:09

Rupesh Pawar