jQuery blur()

The blur event occurs when an element loses focus.The blur() method triggers the blur event, or attaches a function to run when a blur event occurs.

It can be generated by via keyboard commands like tab key or mouse click anywhere on the page.
It makes you enable to attach a function to the event that will be executed when the element loses focus. Originally, this event was used only with form elements like <input>.
The blur () method is often used together with focus () method.

Syntax:
1.      $(selector).blur()  
It triggers the blur event for selected elements.
2.      $(selector).blur(function)  
It adds a function to the blur event.


Example -1
<!DOCTYPE html>  
<html>  
<head>  
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
<script>  
$(document).ready(function(){  
    $("input").blur(function(){  
        alert("This text box has lost its focus.");  
    });  
});  
</script>  
</head>  
<body>  
Enter your name: <input type="text">  
</body>  
</html>  

Example -2
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("input").focus(function(){
        $(this).css("background-color", "#cccccc");
    });
    $("input").blur(function(){
        $(this).css("background-color", "#ffffff");
    });
});
</script>
</head>
<body>
 
Name: <input type="text" name="fullname"><br>
Email: <input type="text" name="email">
 
</body>
</html>


Example -3
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
 
        $("#txtInput1").blur(
             function (event) {
                 $("#txtInput1").css("background-color", "Green");
             }
         );
});
 
</script>
</head>
<body>
 
Name: <input type="text" name="txtInput1"><br>
</body>
</html>