← Back tothis vs that

blur vs focusOut

Written byPhuoc Nguyen
Category
DOM
Created
25 Aug, 2023
In JavaScript DOM, when an element loses focus, two events are triggered: `blur` and `focusOut`. While they may seem alike, there are significant differences between them that are worth noting.

The blur event

The `blur` event happens when an element loses focus, like when the user clicks somewhere else or tabs away from it.
input.addEventListener('blur', () => {
// Executed when element loses focus ...
});
The `blur` event is only triggered on the element that lost focus. It doesn't bubble up the DOM tree, so it won't be triggered on any parent elements.

The focusOut event

The `focusOut` event is also triggered when an element loses focus. However, it behaves slightly differently than `blur`.
element.addEventListener('focusout', () => {
// Executed when element loses focus ...
});
When an `focusOut` event occurs on an element, it bubbles up the DOM tree. This means that if the element has parent elements, those elements will also trigger the `focusOut` event.

The execution orders

It's important to note other difference between the `focusOut` and `blur` events: the `focusOut` event is triggered before the focus shifts, while the `blur` event is dispatched after.
The same goes for the `focusIn` and `focus` events.

Questions? 🙋

Do you have any questions? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.

Recent posts ⚡

Newsletter 🔔

If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn