Categories
JavaScript Answers

How to run JavaScript when an element loses focus?

Spread the love

To run JavaScript when an element loses focus, we set the onblur attribute to the JavaScript code we want to run.

For instance, we write

<input type="text" name="name" value="value" onblur="alert(1);" />

to set the onblur attribute to alert(1); to show an alert box when we move focus away from the input.

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 *