Course contentsShow
← Web Development
Lesson 734 of 2,98213. DOM EventsFree lesson

Input Events: focus, blur, focusin, focusout

Manage focus state with focus/blur events and understand the bubbling variants focusin/focusout.

Input Events: focus, blur, focusin, focusout

You'll learn how to detect and respond when users interact with form fields and other focusable elements.

What is Focus State?

When you click inside an input field or tab to a button, that element becomes "focused"—it's ready to receive your input. Think of focus like a spotlight on a stage: only one actor (element) can stand in the spotlight at a time. When focus moves to another element, the previous one loses focus.

The Core Focus Events

focus fires when an element receives focus (e.g., clicking into a text input). This event does not bubble up the DOM tree.

blur fires when an element loses focus (e.g., clicking outside the input). This also does not bubble.

const input = document.querySelector('input');

input.addEventListener('focus', function() {
  console.log('Input is now focused');
});

input.addEventListener('blur', function() {
  console.log('Input lost focus');
});

The Bubbling Variants

focusin works like focus, but does bubble up the DOM tree. This is useful when you want to detect focus changes on child elements from a parent.

focusout works like blur, but bubbles as well.

const form = document.querySelector('form');

form.addEventListener('focusin', function(event) {
  console.log('Some input inside form received focus:', event.target);
});

form.addEventListener('focusout', function(event) {
  console.log('Some input inside form lost focus:', event.target);
});

When to Use Each

Use focus/blur when attaching listeners directly to the focusable element. Use focusin/focusout when you need event delegation from a parent container.

Key Takeaway: focus and blur detect focus changes but don't bubble; focusin and focusout provide the same functionality with bubbling support for event delegation.