Home > Net >  How do I execute my function whenever the input field loses its focus?
How do I execute my function whenever the input field loses its focus?

Time:09-21

In Javascript, I want to execute my function whenever the textbox loses its focus. What's the easiest way to do this?

CodePudding user response:

So you have two options

OnFocousOut

<input type="text" 
           name="input" 
           value="Lorem"
           onfocusout="yourFunction();" />

or OnBlur

 <input type="text" onblur="myFunction()"> 

https://www.geeksforgeeks.org/implement-a-javascript-when-an-element-loses-focus/

CodePudding user response:

In most of the case I don't use onblur I use onchange method because it will indicate user only if user change the value of text box so i think it's user friendly.

Easiest method is to add your javascript in html tag

   <input type = "text" onchange = "yourFunction()">

but this is not a good practice. Write your javascript in external file.

  • Related