How to detect input type=file "change" for the same file?
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.
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}
Related videos on Youtube
Gadonski
Updated on March 05, 2022Comments
-
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.
- User select file
A.jpg
(event fires) - User select file
B.jpg
(event fires) - User select file
B.jpg
(event doesn't fire, I want it to fire)
How can I do it?
- User select file
-
BrunoLM over 13 yearsIt seems you are just repeating his words and
.click()
is not "fire on re-select". -
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 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 almost 10 yearsi guess you meant something like function() { this.value = null; return false; }
-
fadomire about 9 yearsbe 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 over 8 yearssince "value" is a property and not an attribute of input, the recommended jQuery usage would be
.prop("value", "")
-
iplus26 over 6 years
onClick
fires beforeonChange
, so this method works great for me. -
Jay Dharmendra Solanki over 6 yearsI needed a solution like this. It is really short and gets the work done. Nice thinking Mariusz Wiazowski.
-
songgeb over 6 yearsOther 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 almost 6 yearsCool, way better than removing and adding the element in the dom again, thx
-
Coisox over 5 yearsThank you for this variation answer
-
JohnK over 3 yearsI had to use
$(this).val("");
for jQuery 2.2.4. -
anniex about 3 yearsThanks! Modifying your answer to @click="(e) => e.target.value = null" got me a Vue solution.
-
Alexander Mihailov over 2 yearsthis should be the accepted answer - it requires no javascript at all.
-
Syed Mohammad Sannan over 2 years@AlexanderMihailov It still does, when the user actually selects the file.