Javascript onblur Event Example

Updated on     Kisan Patel

If we want to execute a javascript function when the element loses its focus, we need to use javascript onblur event.

<!-- HTML -->
Name: <input type="text" name="txtName" id="txtName" onblur="TestFunction(" />

<!-- Javascript -->
function TestFunction(id) {
   document.getElementById(id).style.background = "#FFF200";

In the above code snippet when the textbox loses its focus, the onblur event fires and executes TestFunction. In this we are setting the background color of the textbox to “#FFF200”.

See the Pen OVGPxg by Kisan (@pka246) on CodePen.


Leave a Reply