HTML onblur Event Attribute

HTMLWeb DevelopmentFront End Technology

The HTML onblur attribute is used when an HTML element loses focus in an HTML document.


Following is the syntax −

<tagname onblur=”script”></tagname>


Let us see an example of HTML onblur event Attribute −

Live Demo

<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
   body {
      color: #000;
      height: 100vh;
      background-color: #FBAB7E;
      background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%);
      text-align: center;
      padding: 20px;
   .btn {
      background: #db133a;
      border: none;
      height: 2rem;
      border-radius: 2px;
      width: 40%;
      display: block;
      color: #fff;
      outline: none;
      cursor: pointer;
      margin: 1rem auto;
   .show {
      font-size: 1.2rem;
<h1>HTML onblur Event Attribute Demo</h1>
<button onblur="blurFn()" class="btn" onclick='clickFn()'>Click me</button>
<div class="show"></div>
   function blurFn() {
      var msg = document.querySelector('.show');
      msg.innerHTML = "";
      msg.innerHTML = "Now you loses focus from me";
   function clickFn() {
      var msg = document.querySelector('.show');
      msg.innerHTML = "";
      msg.innerHTML = "Hey! you clicked me";


Click on “Click me” button to show some text.

Now click somewhere else and you losses focus from the button.

Updated on 16-Feb-2021 04:43:21