Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events
  • jQuery Effects
  • jQuery Traversing
  • jQuery HTML & CSS
  • jQuery AJAX
  • jQuery Properties
  • jQuery Examples
  • jQuery Interview Questions
  • jQuery Plugins
  • jQuery Cheat Sheet
  • jQuery UI
  • jQuery Mobile
  • jQWidgets
  • Easy UI
  • Web Technology
Open In App
Next Article:
How to download File Using JavaScript/jQuery ?
Next article icon

How to Reset an <input type=”file”> in JavaScript or jQuery

Last Updated : 20 Sep, 2024
Comments
Improve
Suggest changes
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 Content

  • Using jQuery’s wrap() Method
  • Using file.value = ”

1. Using jQuery’s wrap() Method

In 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?

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?

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.


Next Article
How to download File Using JavaScript/jQuery ?

V

Vijay Sirra
Improve
Article Tags :
  • JavaScript
  • JQuery
  • Web Technologies
  • JavaScript-Questions
  • jQuery-Questions

Similar Reads

  • How to Reset a File Input in React.js ?
    To reset a file input in React JS, we can use the useRef Hook to create a reference to the input element and modify its properties using the DOM API. Prerequisites:NPM & Node JSReact JSReact useRef HookApproach:To reset a file input in react we will create a reset button and handleReset function
    2 min read
  • 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 get the input file size in jQuery ?
    The task is to get the fileSize when a user uploads it using JQuery. Approach: Display the text Choose file from system to get the fileSize on the screen. Click on the browse button to select the upload file. After selecting a file, the function is called which display the size of the selected file.
    2 min read
  • How to Change the src Attribute of an Image Element in JavaScript / jQuery?
    To change the src attribute of an image element in JavaScript or jQuery, you can dynamically update the image by setting a new value for the src attribute. Below are the methods to change the src attribute of an image element: Table of Content Using JavaScript DOM methodsUsing jQuery prop() MethodUs
    2 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 Save Data in Local Storage in JavaScript?
    LocalStorage is a client-side web storage mechanism provided by JavaScript, which allows developers to store key-value pairs directly in the user's browser. Unlike cookies, LocalStorage is persistent—data stored remains even after the browser is closed and reopened, making it ideal for retaining use
    2 min read
  • How to read a local text file using JavaScript?
    Reading a local text file involves accessing the contents of a file stored on a user's device. Using JavaScript, this can be achieved with the HTML5 File API and the FileReader object, which reads files selected through an <input> element or drag-and-drop, asynchronously. Getting Started with
    4 min read
  • How to reset selected file with input tag file type in Angular 9?
    To reset the selected file with input tag file type, we can implement a method which on invocation will clear the selected file. Approach: Set the id of your input using '#' to make it readable inside the component.Now in the component we can use ViewChild directive to read the value of the input fr
    2 min read
  • How To Save Text As a File in HTML CSS and JavaScript?
    In this tutorial, we will learn how to save user input as a text file using HTML, CSS, and JavaScript. This can be particularly useful for web applications where users need to download their input or notes as a file. Project PreviewWe will build a simple web page that allows users to enter text in a
    2 min read
  • How to change the <input> type?
    Changing the <input> type in HTML involves dynamically modifying the type attribute of an <input> element using JavaScript or jQuery. By altering the type (e.g., from text to password), the behavior and appearance of the input field are instantly updated. Syntax: The selected input type
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences