Teifion
Teifion

Reputation: 111109

Run JavaScript when an element loses focus

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);"/>

Upvotes: 146

Views: 193113

Answers (6)

William Hou
William Hou

Reputation: 1781

Just want to mention if it's Angular:

Angular doesn't take onblur or onfocusout. It ONLY takes (blur):

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

Upvotes: 0

kurdtpage
kurdtpage

Reputation: 3221

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

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

Upvotes: 0

Lo&#239;c Wolff
Lo&#239;c Wolff

Reputation: 3312

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

Upvotes: 25

Gumbo
Gumbo

Reputation: 655755

onblur is the opposite of onfocus.

Upvotes: 73

Dan Lew
Dan Lew

Reputation: 87430

You want to use the onblur event.

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

Upvotes: 38

Canavar
Canavar

Reputation: 48098

How about onblur event :

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

Upvotes: 276

Related Questions