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

140,779

Solution 1

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

Solution 2

You can simply set to null the file path every time user clicks on the control. Now, even if the user selects the same file, the onchange event will be triggered.

<input id="file" onchange="file_changed(this)" onclick="this.value=null;" type="file" accept="*/*" />

Solution 3

If you have tried .attr("value", "") and didn't work, don't panic (like I did)

just do .val("") instead, and will work fine

Solution 4

Use onClick event to clear value of target input, each time user clicks on field. This ensures that the onChange event will be triggered for the same file as well. Worked for me :)

onInputClick = (event) => {
    event.target.value = ''
}

<input type="file" onChange={onFileChanged} onClick={onInputClick} />

Using TypeScript

onInputClick = ( event: React.MouseEvent<HTMLInputElement, MouseEvent>) => {
    const element = event.target as HTMLInputElement
    element.value = ''
}

Solution 5

Here's the React-y way solution i've found that worked for me:

onClick={event => event.target.value = null}

Share:
140,779

Related videos on Youtube

Gadonski
Author by

Gadonski

Updated on March 05, 2022

Comments

  • Gadonski
    Gadonski over 2 years

    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?

  • BrunoLM
    BrunoLM over 13 years
    It seems you are just repeating his words and .click() is not "fire on re-select".
  • Nick Craver
    Nick Craver over 13 years
    @BrunoLM - No, it's not...but I think you read over the portion where I say you can't do this, the click is as close as it gets.
  • BrunoLM
    BrunoLM over 13 years
    @Pekka: Not exactly. But he can just adapt it. Let's say he needs to post the file. After the post he can call a js function that will remove and add the new file selector. It is doable.
  • Jacek Pietal
    Jacek Pietal almost 10 years
    i guess you meant something like function() { this.value = null; return false; }
  • fadomire
    fadomire about 9 years
    be aware that when you use .attr("value", "") or .val("") (like suggested by @wagner-leonardi below) internet explorer will fire the change event while chrome don't
  • Roger Barreto
    Roger Barreto over 8 years
    since "value" is a property and not an attribute of input, the recommended jQuery usage would be .prop("value", "")
  • iplus26
    iplus26 over 6 years
    onClick fires before onChange, so this method works great for me.
  • Jay Dharmendra Solanki
    Jay Dharmendra Solanki over 6 years
    I needed a solution like this. It is really short and gets the work done. Nice thinking Mariusz Wiazowski.
  • songgeb
    songgeb over 6 years
    Other solution is that changing value when input tag changed. But We do not expect the value be changed after select a file.
  • David Dal Busco
    David Dal Busco almost 6 years
    Cool, way better than removing and adding the element in the dom again, thx
  • Coisox
    Coisox over 5 years
    Thank you for this variation answer
  • JohnK
    JohnK over 3 years
    I had to use $(this).val(""); for jQuery 2.2.4.
  • anniex
    anniex about 3 years
    Thanks! Modifying your answer to @click="(e) => e.target.value = null" got me a Vue solution.
  • Alexander Mihailov
    Alexander Mihailov over 2 years
    this should be the accepted answer - it requires no javascript at all.
  • Syed Mohammad Sannan
    Syed Mohammad Sannan over 2 years
    @AlexanderMihailov It still does, when the user actually selects the file.