How to detect input type=file "change" for the same file?

Gadonski picture Gadonski · Nov 5, 2010 · Viewed 104.1k times · Source

I want to fire an event when the user select a file. Doing so with .change event it works if the user changes the file every time.

But I want to fire the event if the user select the same file again.

  1. User select file A.jpg (event fires)
  2. User select file B.jpg (event fires)
  3. User select file B.jpg (event doesn't fire, I want it to fire)

How can I do it?

Answer

BrunoLM picture BrunoLM · Nov 7, 2010

You can trick it. Remove the file element and add it in the same place on change event. It will erase the file path making it changeable every time.

Example on jsFiddle.

Or you can simply use .prop("value", ""), see this example on jsFiddle.

  • jQuery 1.6+ prop
  • Earlier versions attr