Run JavaScript when an element loses focus

JavascriptHtmlDom Events

Javascript Problem Overview


I have a standard HTML input that I want to run JavaScript code when it loses focus. Sadly my Google searches did not reveal how to do this.

To make it clear, I'm looking for a way to do this:

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

Javascript Solutions


Solution 1 - Javascript

How about onblur event :

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

Solution 2 - Javascript

onblur is the opposite of onfocus.

Solution 3 - Javascript

You want to use the onblur event.

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

Solution 4 - Javascript

You're looking for the onblur event. Look here, for more details.

Solution 5 - Javascript

From w3schools.com: Made compatible with Firefox Sept, 2016

<input type="text" onfocusout="myFunction()">

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTeifionView Question on Stackoverflow
Solution 1 - JavascriptCanavarView Answer on Stackoverflow
Solution 2 - JavascriptGumboView Answer on Stackoverflow
Solution 3 - JavascriptDan LewView Answer on Stackoverflow
Solution 4 - JavascriptLoïc WolffView Answer on Stackoverflow
Solution 5 - JavascriptkurdtpageView Answer on Stackoverflow