0

This was discussed a lot, but all of these solutions not work:

$("#getFile").val('');

Nor this: document.getElementById("getFile").value = "";

When console.log after using one of this - it prints the value of last file attached. Also cloning not works, although remove the input value.

I want to trigger the event "change" when the user uses the same file after removing. The problem is thatthe value of the input is the last file uploaded.

Eyal Segal
  • 41
  • 2
  • 9

2 Answers2

0

why not just track the value with another variable and do an if statement? Like var lastUpload = document.getElementById("getFile").value;

then

addEventListener("change", function() {
  if(lastUpload === document.getElementById("getFile").value) {
    ...
  }
);
Rumba
  • 181
  • 1
  • 2
  • 9
0

I think this is only possible if you can reset the form as well, see below:

 <form action="#" id="form">
     <label>Choose File: </label> <input type="file" name="getFile" id="getFile" /><br />
     <button type="button" onClick="removeFileName()">Clear File</button>
 </form>

And JS:

function removeFileName(){
    form.reset();
    var field = document.getElementById("getFile");
    console.log(field.value);
}
Hanif
  • 3,739
  • 1
  • 12
  • 18
  • yes! thank you! (but also using "click" event instead, or - although this is not necessary - use settimeout) – Eyal Segal Mar 04 '18 at 06:12