0

As you see in the screenshot. I choose a file, leave the pop up without refreshing the page and open it again, and the file input still has the file I picked.

<input id="fileuploadbannermobile" type="file" name="files[]">

I try this when the pop up opened.

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

But it doesn't seem to solve the issue.

screenshot

romellem
  • 5,792
  • 1
  • 32
  • 64
Mert
  • 1
  • 1
  • Could you create a runnable snippet (and test that it does show the problem) thanks. See https://stackoverflow.com/help/minimal-reproducible-example – A Haworth Dec 10 '22 at 20:52
  • Also, use trigger('change'); Like :: $("#file").val('').trigger('change'); – Felix Tenn Dec 10 '22 at 21:05
  • The code you have [should work fine](https://stackoverflow.com/questions/9617738/how-to-clear-file-input). If it doesn't, it is something else you haven't shown here causing problems. How/when do you fire the reset? – Don't Panic Dec 10 '22 at 23:13

1 Answers1

1

Setting an input value to empty string or null should definitely work.

  • try setting a value to "" or null in vanila javascript

    yourInput.value = null or yourInput.value = ""

  • also check if you are setting those values to the right input

It would also be useful to know what browser you use.

  • thats my file input btw. maybe other attributes are the problem – Mert Dec 10 '22 at 21:27
  • @Mert first, name can not be a list. I prepared you a fiddle, try it: [Fiddle link](https://jsfiddle.net/pf68sgzv/) – Andrej Novák Dec 10 '22 at 21:40
  • @AndrejNovák Why do you think plain JS will work when jQuery does not, I mean what do you think the problem is? Also, a `name` with square brackets like `files[]` is perfectly valid ([example](https://stackoverflow.com/questions/1175347/how-can-i-select-and-upload-multiple-files-with-html-and-php-using-http-post), [example](https://stackoverflow.com/questions/12854259/square-brackets-in-name-attribute-of-input-tag), [example](https://stackoverflow.com/questions/7946450/why-do-i-use-brackets-in-the-name-attribute-of-input-element), ...) – Don't Panic Dec 10 '22 at 23:11
  • @Don'tPanic you are right, i completely forget about that. "what do you think the problem is?" - at this point i can not be certain. Author did not provided the code where he is registering/executing events (during pop up opening), my guess is that the value setting is not triggered in any way. – Andrej Novák Dec 10 '22 at 23:20