How to detect when cancel is clicked on file input using JavaScript ? Last Updated : 11 Jun, 2020 Comments Improve Suggest changes Like Article Like Report 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 file for its functionality. Note: This method works best with WebKit browsers such as Google Chrome and Safari. It may not work reliably with Mozilla Firefox. Example: HTML Code: The following code is defines in the HTML of the page. html <input type='file' id='theFile' onclick="initialize()" /> Explanation: The property value type="file" tells that the type of input the user enters is a file.The property value id="theFile" is used to link the JavaScript code to it using getElementById() method.The property value onclick="initialize()" is used to specify the function call when the user has clicked on the input. JavaScript Code: javascript // Get the file input element var theFile = document.getElementById('theFile'); // Define a function to be called // when the input is focused function initialize() { document.body.onfocus = checkIt; console.log('initializing'); } // Define a function to check if // the user failed to upload file function checkIt() { // Check if the number of files // is not zero if (theFile.value.length) { alert('Files Loaded'); } // Alert the user if the number // of file is zero else { alert('Cancel clicked'); } document.body.onfocus = null; console.log('checked'); } Explanation for the initialize() function: The variable theFile is created and the input element is selected using its id.The "document.body.onfocus = checkIt" line defines that the onfocus event is fired when an element gets focused. Explanation for checkIt() function: When the user uploads any file, the length of the files is found using theFile.value.length property. As this value becomes non-zero, the condition gets satisfied and an alert box appears that shows "Files Loaded".When the user does not upload a file, then the length of the files is a zero value. Due to this, the condition does not get satisfied and an alert box appears that shows "Cancel clicked". Complete Code: In this section, we will combine the above two sections of code to detect when cancel is clicked on file input. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <title> How to detect when cancel is clicked on file input? </title> </head> <body> <input type='file' id='theFile' onclick="initialize()" /> <script> // Get the file input element var theFile = document.getElementById('theFile'); // Define a function to be called // when the input is focused function initialize() { document.body.onfocus = checkIt; console.log('initializing'); } // Define a function to check if // the user failed to upload file function checkIt() { // Check if the number of files // is not zero if (theFile.value.length) { alert('Files Loaded'); } // Alert the user if the number // of file is zero else { alert('Cancel clicked'); } document.body.onfocus = null; console.log('checked'); } </script> </body> </html> Output: File input for the user to select a file: Alert when the user has failed to input the file: On successful upload, the file name appears as intended: Comment More infoAdvertise with us Next Article How to detect when cancel is clicked on file input using JavaScript ? S samuelcoder365 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to check input file is empty or not using JavaScript/jQuery ? Given an HTML document containing an input element, the task is to check whether an input element is empty or not with the help of JavaScript. These are the two approaches to check input file is empty or not using JavaScript/jQuery: Table of Content Using element.files.length property in JavaScript 2 min read How to Trigger a File Download when Clicking an HTML Button or JavaScript? Triggering file downloads in JavaScript refers to initiating the download of files directly from a webpage when a user clicks a button or link. This can be achieved using HTML5's download attribute or custom JavaScript functions, allowing dynamic and user-friendly file-downloading experiences.To tri 4 min read How to create a File Input using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a File Input Area using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ hr 1 min read Remove the Chrome's "No file chosen option" from a file input using JavaScript The task is to remove the "no file chosen" value from the empty input element in chrome. We are going to achieve that task with the help of JavaScript. Approach 1: Remove the tooltip value('No file chosen'). Use .attr() method to set the value of title attribute to empty. Example 1: This example rem 2 min read How to detect browser or tab closing in JavaScript ? Detecting browser or tab closure in JavaScript is essential for preventing data loss or unintended navigation. Using the beforeunload event, developers can prompt users with a confirmation dialog, ensuring they don't accidentally leave a page with unsaved changes or important information. The before 2 min read How to Check Mentioned File Exists or not using JavaScript/jQuery? Sometimes we want to upload a file through the path of the file, but a few times the file does not exist. In that case, we have to respond to users the given path does not find any file or does not exist in the mentioned file. Below are the approaches to check if mentioned file exists or not using J 3 min read How to download File Using JavaScript/jQuery ? The ability to download files directly from a website is an essential feature for many web applications. Whether you're providing documents, images, or other data, enabling users to download files seamlessly enhances their experience and ensures they can access the content offline. This article prov 2 min read How to programmatically fire a click event for a file input element in JavaScript ? In this article, we will learn how to programmatically fire click events on the input file element.ApproachWhenever you want to perform a click event programmatically, at your specific condition, just use the JavaScript in-built click() function by DOM object.Example:document.getElementById('your_in 2 min read Create a Disable Right Click, Copy, Cut & Paste using JavaScript In this article, we have created a simple user interface using HTML and CSS, where there is an input field on which Right-Click, Copy, Cut, and Paste counts are displayed. When the user tries to perform these operations, a warning or error message is displayed, and the counter value is incremented f 3 min read How to set a value to an input file using HTML? In HTML, we will use the type attribute to take input in a form and when we have to take the file as an input, the file value of the type attribute allows us to define an element for the file uploads. It displays a browse button on our computer screen, and when we click on it, it asks the user for p 2 min read Like