jQuery Blur Event Example

jQuery Blur Event Example

jQuery blur event occurs when element loses focus. It can be generated anywhere by tabs or mouse commands, such as mouse clicks anywhere on the page.

Syntax

$(selector).blur()  

This triggers the blur event for the selected elements.

$(selector).blur(function)  

Parameters of jQuery blur() events

ParameterDescription
FunctionIt is an optional parameter. It is used to specify the function to run when the element loses the focus (blur).

Example of jQuery blur() Event

Let’s take an example for demonstrate jQuery blur().

<!DOCTYPE html>  
<html>  
<head>  
<script src="https://code.jquery.com/jquery-3.3.1.min.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>  

Let’s take an example for demonstrate jQuery blur().

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>  
$(document).ready(function(){  
 $( "#field1" ).blur(function() {
  alert( "Lose focus from Field1" );
});
});  
</script>  
  <meta charset="utf-8">
  <title>jQuery: Attach a function to the blur event</title>
</head>
<body>
<form>
<input id="field1" type="text" value="First Field">
<input id="field2" type="text" value="Second Field">
</form>
  <p>Write something in the input First Field, and then click outside the field to check lose focus.</p>
</body>
</html>

The jQuery events (handler) method is used to bind an event handler to a “blur” javascript event, or an event that is triggered.

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *