jQuery blur()

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

jQuery blur()

The jQuery blur event occurs when element loses focus. 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>. In latest browsers, it has been extended to include all element types.

The blur () method is often used together with focus () method.

Syntax:

  1. $(selector).blur()  

It triggers the blur event for selected elements.

  1. $(selector).blur(function)  

It adds a function to the blur event.

Parameters of jQuery blur() event

Parameter Description
Function It 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 to demonstrate jQuery blur() event.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
  5. <script>  
  6. $(document).ready(function(){  
  7.     $("input").blur(function(){  
  8.         alert("This text box has lost its focus.");  
  9.     });  
  10. });  
  11. </script>  
  12. </head>  
  13. <body>  
  14. Enter your name: <input type="text">  
  15. </body>  
  16. </html>  

Test it Now

Output:

Enter your name: 

Note: Write your name in the input field, and then click outside the field to lose focus (blur).