How to Reset an <input type="file"> in JavaScript or jQuery Last Updated : 20 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Sometimes, you may want to clear a file that has been selected by the user in an <input type="file"> field, without having to reload the entire page. This can be useful if the user wants to reselect a file or if you need to reset a form.There are two main ways to reset a file input in JavaScript/jQuery:Table of ContentUsing jQuery’s wrap() MethodUsing file.value = ''1. Using jQuery’s wrap() MethodIn this approach, we temporarily wrap the file input element inside a <form> element, then reset the form. By calling form.reset(), we can reset the file input, clearing any selected files.Example: In this example, the wrap() method temporarily wraps the file input (#infileid) inside a form element. This allows the reset() method to clear the file input. Afterward, the unwrap() method removes the temporary form. HTML <!DOCTYPE html> <html> <head> <title>Using wrap Method</title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <!-- jQuery code to show the working of this method --> <script> $(document).ready(function () { $('#resetbtn').on('click', function (e) { let $el = $('#infileid'); $el.wrap('<form>').closest( 'form').get(0).reset(); $el.unwrap(); }); }); </script> </head> <body> <h2 style="color:green">GeeksforGeeks</h2> <form id="find" name="formname"> <p> <label>File</label> <input id="infileid" type="file"> </p> <p> <button id="resetbtn" type="button"> Reset file </button> </p> </form> </body> </html> Output:How to reset input type = "file" using JavaScript/jQuery?Explanation:The file input (<input type="file" id="infileid">) is temporarily wrapped in a form using $('#infileid').wrap('<form>').We call form.reset() on the wrapped form to clear the file input.After resetting, the file input is "unwrapped" from the temporary form using $('#infileid').unwrap().2. Using file.value = ''This is a simpler and more direct approach. You can reset the file input by setting its value property to an empty string, which removes any selected files.Example: In this example, we use JavaScript to reset a file input. The resetFile() function targets the file input element with the class .file and clears its value, effectively resetting the file selection. HTML <!DOCTYPE html> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <!-- jQuery code to show the working of this method --> <script> function resetFile() { const file = document.querySelector('.file'); file.value = ''; } </script> </head> <body> <h2 style="color:green"> GeeksforGeeks </h2> <input type="file" class="file" /> <br> <button onclick="resetFile()"> Reset file </button> </body> </html> Output:How to reset input type = "file" using JavaScript/jQuery?Explanation:The file input (<input type="file" class="file">) is selected using document.querySelector().By setting the value property of the file input to an empty string (''), the file selection is cleared, and the input is reset. Comment More infoAdvertise with us Next Article How to Reset an <input type="file"> in JavaScript or jQuery V Vijay Sirra Follow Improve Article Tags : JQuery JavaScript-Questions jQuery-Questions Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi 6 min read What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac 13 min read Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i 6 min read Like