HTML | DOM onblur Event
The HTML DOM onblur event occurs when an object loses focus. The onblur event is the opposite of the onfocus event.
The onblur event is mostly used with form validation code (e.g. when the user leaves a form field).
Syntax:
In HTML:
<element onblur="myScript">
In JavaScript:
object.onblur = function(){myScript};
In JavaScript, using the addEventListener() method:
object.addEventListener("blur", myScript);
Example: Using HTML
<!DOCTYPE html> <html> <body> <center> <h1 style="color:green"> GeeksforGeeks </h1> <h2>HTML DOM onblur event</h2> Email: <input type="email" id="email" onblur="myFunction()"> <script> function myFunction() { alert("Focus lost"); } </script> </center> </body> </html> |
Output:


Example: In JavaScript:
<!DOCTYPE html> <html> <body> <center> <h1 style="color:green"> GeeksforGeeks </h1> <h2>HTML DOM onblur event</h2> <input type="email" id="email"> <script> document.getElementById("email").onblur = function() { myFunction() }; function myFunction() { alert("Input field lost focus."); } </script> </center> </body> </html> |
Output:


Example: In JavaScript, using the addEventListener() method
<!DOCTYPE html> <html> <body> <center> <h1 style="color:green"> GeeksforGeeks </h1> <h2>HTML DOM onblur event</h2> <input type="email" id="email"> <script> document.getElementById( "email").addEventListener("blur", myFunction); function myFunction() { alert("Input field lost focus."); } </script> </center> </body> </html> |
Output:


Supported Browsers: The browsers supported by HTML DOM onblur Event are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Apple Safari
- Opera
Recommended Posts:
- HTML | onblur Attribute
- HTML | DOM onmousemove Event
- HTML | DOM onmouseover event
- HTML | DOM ononline Event
- HTML | DOM onoffline Event
- HTML | DOM onmouseout Event
- HTML | DOM onmousedown Event
- HTML | DOM onmouseenter Event
- HTML | DOM onmouseleave Event
- HTML | DOM oncontextmenu Event
- HTML | DOM ondragstart Event
- HTML | DOM onpagehide Event
- HTML | DOM ondurationchange Event
- HTML | DOM onpageshow Event
- HTML | DOM ondrop Event
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



