Sometimes, we want to run JavaScript when an element loses focus.
In this article, we’ll look at how to run JavaScript when an element loses focus.
How to run JavaScript when an element loses focus?
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 call alert
with 1 when we move focus away from the input.
Conclusion
To run JavaScript when an element loses focus, we set the onblur
attribute to the JavaScript code we want to run.