Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7807361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:50:03+00:00 2026-06-02T02:50:03+00:00

I have simple piece of code to upload files: $(document).ready(function () { $(.attachmentsUpload input.file).change(function

  • 0

I have simple piece of code to upload files:

$(document).ready(function () {
    $(".attachmentsUpload input.file").change(function () {
        $('form').submit();
    });
});

<form class="attachmentsUpload" action="/UploadHandler.ashx" method="post" enctype="multipart/form-data">
    <input type="file" class="file" name="file" />
</form>

While I click on input and then select a file in dialog box, I’m submitting this file using ajax. This is not important part here. Important part is, that while I select the same file second time in the dialog box, just after submitting the first file, the .change() event does not fire in IE and Chrome. But while I choose different file, the event fires and works properly. Under Firefox it is firing all the time.

How to workaround this, to work as expected (as in Firefox) ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:50:05+00:00Added an answer on June 2, 2026 at 2:50 am

    Description

    This happens because the value of the input field (the selected filepath) does not change if you select the same file again.

    You can set the value in the onChange() event to an empty string and submit your form only if the value is not empty. Have a look at my sample and this jsFiddle Demonstration.

    Sample

    $(".attachmentsUpload input.file").change(function () {
        if ($(".attachmentsUpload input.file").val() == "") {
            return;
        }
        // your ajax submit
        $(".attachmentsUpload input.file").val("");
    });
    

    Update

    This, for any reason, does not work in IE9. You can replace the element to reset them.
    In this case you need jQuery live() to bind the event, because your input field will dynamically (re)created.
    This will work in all browsers.

    I found this solution on the stackoverflow answer Clearing input type=’file’ using jQuery

    $(".attachmentsUpload input.file").live("change", function () {
        if ($(".attachmentsUpload input.file").val() == "") {
            return;
        }
        // get the inputs value
        var fileInputContent = $(".attachmentsUpload input.file").val();
        // your ajax submit
        alert("submit value of the file input field=" + fileInputContent);
        // reset the field
        $(".attachmentsUpload input.file").replaceWith('<input type="file" class="file" name="file" />');
    });​
    

    More Information

    • jQuery.live()
    • jQuery.replaceWith()

    Check out my updated jsFiddle

    Note: live is now removed from later versions of jQuery. Please use on instead of live.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this simple piece of code in jQuery $(document).ready(function() { $('#switcher').click(function(event) { if
I have this simple piece of HTML code: <div> <input type=file name=english-file /> </div>
I have to write a simple piece of code that acts on a file;
I have this simple piece of code on a windows form containing said DataGridView
I have this simple piece of code: MyObjectContext db = new MyObjectContext(); Person new_person
I have a very simple piece of code: #include <stdio.h> #include <glib.h> int main(int
I've wrote this simple piece of code. And I have a slight problem with
I have simple form. <form target=_blank action=somescript.php method=Post id=simpleForm> <input type=hidden name=url value=http://...> <input
I have a simple piece of code written to open a report. Private Sub
I have a very simple little piece of Lua code, which I wrote while

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.