HTML event attribute onblur

The onblur attribute is triggered when the element lost focus.

What's new in HTML5



<element onblur="script">

Supported Tags

All HTML elements, EXCEPT:


Browser compatibility

onblur Yes Yes Yes Yes Yes


<!DOCTYPE html>
<!--   w ww .ja v  a  2s . c  o m-->
Enter your first name: <input type="text" name="fname" id="fname" onblur="myFunction()">
Enter your last name: <input type="text" name="lname" id="lname" onblur="myFunction()">

function myFunction() {
    console.log("lost focus");


Click to view the demo

Example 2

The following code shows how to listen to Focus Lost event for input password.

<script type="text/javascript">
  function checkRequired() {<!-- www.  j  a  v a2s  . co m-->
    document.write("value is required in field");
  <form name="someForm">
    <input type="text" name="text1" /><br /> 
    <input type="password" name="text2" onblur='checkRequired();'/><br /> 
    <input type="hidden" name="text3" value="hidden value" />
    <textarea name="text4" cols=50 rows=10>The text area</textarea>
    <input type="submit" value="Submit" />

Click to view the demo