← Back tothis vs that

blur vs focusOut

Written byPhuoc Nguyen
Created
25 Aug, 2023
Category
DOM
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.
js
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`.
js
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.
If you found this post helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

Questions? 🙋

Do you have any questions about front-end development? If so, feel free to create a new issue on GitHub using the button below. I'm happy to help with any topic you'd like to learn more about, even beyond what's covered in this post.
While I have a long list of upcoming topics, I'm always eager to prioritize your questions and ideas for future content. Let's learn and grow together! Sharing knowledge is the best way to elevate ourselves 🥷.
Ask me questions

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