Implement a JavaScript when an element loses focus Last Updated : 19 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a document, the task is to implement functionality when the element loses focus. We have 2 options, one is the onblur event and another is onfocusout event JavaScript. We're going to discuss a few methods. First few methods to understand. Approach 1: Using onblur event JavaScript onblur Event: This event happens when an element is going to lose focus. In HTML <element onfocusout="script"> In JavaScript object.onfocusout = function(){script}; In JavaScript, with the addEventListener() method object.addEventListener("blur", script); Example 1: This example adds an onblur event to the <input> element and when it happens the specified code runs. html <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"> </p> <input type="text" name="input" value="inputElement" onblur="gfg_Run();" /> <p id="GFG_DOWN"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var today = 'First click inside of input'+ ' and then outside to perform event!'; el_up.innerHTML = today; function gfg_Run() { el_down.innerHTML = "Input element lost focus"; } </script> Output: Implement a JavaScript when an element loses focus Approach 2: Using onfocusout event JavaScript onfocusout Event: This method appends a node as the last child of a node. Syntax: In HTML <element onfocusout="script"> In JavaScript object.onfocusout = function(){script}; In JavaScript, with the addEventListener() method object.addEventListener("focusout", script); Example 2: This example adds an onfocusout event to the <input> element and when it happens the specified code runs. html <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"> </p> <input type="text" name="input" value="inputElement" onfocusout="gfg_Run();" /> <p id="GFG_DOWN"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var today = 'First click inside of input'+ ' and then outside to perform event!'; el_up.innerHTML = today; function gfg_Run() { el_down.innerHTML = "Input element lost focus"; } </script> Output: Implement a JavaScript when an element loses focus Comment More infoAdvertise with us Next Article How to detect when cancel is clicked on file input using JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads Set the focus to HTML form element using JavaScript To set focus to an HTML form element, there can be many methods that can be used to focus. In this article, we will discuss them all. These are the following approaches: Table of Content Using focus() methodUsing window.onload methodApproach 1: Using focus() methodIn this approach, we are going to u 2 min read How to detect when cancel is clicked on file input using JavaScript ? This article describes the method to handle a situation when the user tries to input a file and later fails to upload the file. It gives the status to the user that the file is not uploaded. This may be useful in situations when an important file needs to be submitted or the application requires the 3 min read How to focus element while loading the page in HTML ? HyperText Markup Language(HTML) is the basic building block of web pages that defines a structure. This markup language is used by browsers to manipulate data like text, images, and other content so that they can be displayed in the required format. HyperText refers to the links that connect web pag 3 min read How to pre-select an input element when the page loads in HTML5? In this article, we are going to pre-select an input element when the page loads i.e. the user doesn't have to click the first text box to start writing on it. When the page is loaded the input is to be pre-selected and the user can start typing directly. This can be done by using the autofocus attr 1 min read Javascript Window Blur() and Window Focus() Method JavaScript provides various methods to manipulate browser windows, two of which are window.blur() and window.focus(). These methods are particularly useful when managing the user interface and enhancing the user experience by controlling window focus.Javascript Window Blur()Javascript Window Blur() 3 min read How to set a keygen element that automatically get focused when page loads in HTML5 ? The <keygen> tag in HTML is used to specify a key-pair generator field in a form. The purpose of this element is to provide a secure way to authenticate users. When a form is submitted then two keys are generated, private key and public key. The private key is stored locally, and the public ke 1 min read Like