Is it possible to clear an <input type='file' />
control value with jQuery? I've tried the following:
$('#control').attr({ value: '' });
But it's not working.
Is it possible to clear an <input type='file' />
control value with jQuery? I've tried the following:
$('#control').attr({ value: '' });
But it's not working.
Easy: you wrap a
<form>
around the element, call reset on the form, then remove the form using unwrap(). Unlike the clone() solutions otherwise in this thread, you end up with the same element at the end (including custom properties that were set on it).Tested and working in Opera, Firefox, Safari, Chrome and IE6+. Also works on other types of form elements, with the exception of
type="hidden"
.http://jsfiddle.net/rPaZQ/
As Timo notes below, if you have the buttons to trigger the reset of the field inside of the
<form>
, you must call preventDefault on the event to prevent the<button>
from triggering a submit.Edit
Does not work in IE 11 due to an unfixed bug. The text (file name) is cleared on the input, but its
File
list remains populated.I have managed to get this to work using the following...
This has been tested in IE10, FF, Chrome & Opera.
There are two caveats...
Still doesn't work properly in FF, if you refresh the page, the file element gets re-populated with the selected file. Where it is getting this info from is beyond me. What else related to a file input element could I possible try to clear?
Remember to use delegation on any events you had attached to the file input element, so they still work when the clone is made.
What I don't understand is who on earth thought not allowing you to clear an input field from an invalid unacceptable file selection was a good idea?
OK, don't let me dynamically set it with a value so I can't leach files from a user's OS, but let me clear an invalid selection without resetting an entire form.
It's not like 'accept' does anything other than a filter anyhow and in IE10, it doesn't even understand MS Word mime types, it's a joke!
Quick answer: replace it.
In the code below I use the
replaceWith
jQuery method to replace the control with a clone of itself. In the event you have any handlers bound to events on this control, we'll want to preserve those as well. To do this we pass intrue
as the first parameter of theclone
method.Fiddle: http://jsfiddle.net/jonathansampson/dAQVM/
If cloning, while preserving event handlers, presents any issues you could consider using event delegation to handle clicks on this control from a parent element:
This prevents the need for any handlers to be cloned along with the element when the control is being refreshed.
The value of file inputs is read only (for security reasons). You can't blank it programatically (other than by calling the reset() method of the form, which has a broader scope than just that field).
You can replace it with its clone like so
But this clones with its value too so you had better like so
The
.clone()
thing does not work in Opera (and possibly others). It keeps the content.The closest method here for me was Jonathan's earlier, however ensuring that the field preserved its name, classes, etc made for messy code in my case.
Something like this might work well (thanks to Quentin too):