How to reset input type = “file” using JavaScript/jQuery?

Jquery:

  • Using wrap method in jquery:
    The best way to reset input type=file is reseting the whole form. Wrap <input type = “file”> into <form> tags and reset the form:
    Example-1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>reset input type = “file”</title>
        <script src=
        </script>
      
        <!-- jQuery code to show the
             working of this method -->
        <script>
            $(document).ready(function() {
                $('#resetbtn').on('click', function(e) {
                    var $el = $('#infileid');
                    $el.wrap('<form>').closest(
                      'form').get(0).reset();
                    $el.unwrap();
                });
            });
        </script>
    </head>
      
    <body>
        <center>
            <h2 style="color:green">GeeksforGeeks</h2>
            <form id="fid" name="formname">
                <p>
                    <label>File</label>
                    <input id="infileid" type="file">
                </p>
                <p>
                    <button id="resetbtn" 
                            type="button">
                      Reset file
                  </button>
                </p>
            </form>
        </center>
    </body>
      
    </html>

    chevron_right

    
    

    Output:
    Before:

    After:

  • Using file.value = ”:
    The simplest way to reset the input is changing the filled value with nothing. this method is workes in every browser.
    Example-2:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            reset input type = “file”
        </title>
        <script src=
        </script>
      
        <!-- jQuery code to show the
             working of this method -->
        <script>
            function resetFile() {
                const file =
                    document.querySelector('.file');
                file.value = '';
            }
        </script>
    </head>
      
    <body>
        <center>
            <h2 style="color:green">
        GeeksforGeeks
      </h2>
            <input type="file" class="file" />
            <button onclick="resetFile()">
                Reset file
            </button>
        </center>
    </body>
      
    </html>

    chevron_right

    
    

    Output:
    Before:

    After:

  • JavaScript:

  • Using wrap method in jquery:
    Wrap <input type = “file”> in to <form> tags and reset the form:
    Example-3:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
          reset input type = “file”
      </title>
      
    </head>
      
    <body>
        <center>
            <h2 style="color:green">
              GeeksforGeeks
          </h2>
            <form id="fid" 
                  name="formname">
                <p>
                    <label>File</label>
                    <input type="file">
                </p>
                <p>
                    <button id="resetbtn" 
                            type="button" 
                            onClick="this.form.reset()">
                      Reset file
                  </button>
                </p>
            </form>
        </center>
    </body>
      
    </html>

    chevron_right

    
    

    Output:
    Before:

    After:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.