Categories
JavaScript Answers

How to catch the moment the input box lost focus with JavaScript?

Spread the love

Sometimes, we want to catch the moment the input box lost focus with JavaScript.

In this article, we’ll look at how to catch the moment the input box lost focus with JavaScript.

How to catch the moment the input box lost focus with JavaScript?

To catch the moment the input box lost focus with JavaScript, we can listen for the blur event.

For instance, we write:

<input>

to add an input.

Then we write:

const input = document.querySelector('input')
input.onblur = () => {
  console.log('blurred')
}

to select the input with querySelector.

And then we set input.onblur to a function that logs 'blurred'.

Therefore, when we move focus away from the input, we see 'blurred' logged.

Conclusion

To catch the moment the input box lost focus with JavaScript, we can listen for the blur event.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *