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
But it's not working. |
|||||
|
Quick answer: replace it. In the code below I use the
|
|||||||||||||||||||||
|
This solution is more elegant than cloning the input element. You wrap a Tested and working in Opera, Firefox, Safari, Chrome and IE6+. Also works on other types of form elements, with the exception of type="hidden".
As Timo notes below, if you have the buttons to trigger the reset of the field inside of the |
|||||||||||||
|
In IE8 they made the File Upload field read-only for security. See the IE team blog post:
|
|||
|
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). |
|||||||||||
|
I ended up with this:
may not be the most elegant solution, but it work as far as I can tell. |
|||
|
The 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):
|
|||
I have used https://github.com/malsup/form/blob/master/jquery.form.js, which has a function called The usage is easy: // Clear all file fields: $("input:file").clearInputs(); // Clear also hidden fields: $("input:file").clearInputs(true); // Clear specific fields: $("#myfilefield1,#myfilefield2").clearInputs(); /** * Clears the selected form elements. */ $.fn.clearFields = $.fn.clearInputs = function(includeHidden) { var re = /^(?:color|date|datetime|email|month|number|password|range|search|tel|text|time|url|week)$/i; // 'hidden' is not in this list return this.each(function() { var t = this.type, tag = this.tagName.toLowerCase(); if (re.test(t) || tag == 'textarea') { this.value = ''; } else if (t == 'checkbox' || t == 'radio') { this.checked = false; } else if (tag == 'select') { this.selectedIndex = -1; } else if (t == "file") { if (/MSIE/.test(navigator.userAgent)) { $(this).replaceWith($(this).clone(true)); } else { $(this).val(''); } } else if (includeHidden) { // includeHidden can be the value true, or it can be a selector string // indicating a special test; for example: // $('#myForm').clearForm('.special:hidden') // the above would clean hidden inputs that have the class of 'special' if ( (includeHidden === true && /hidden/.test(t)) || (typeof includeHidden == 'string' && $(this).is(includeHidden)) ) this.value = ''; } }); }; |
||||
|
For obvious security reasons you can't set the value of a file input, even to an empty string. All you have to do is reset the form where the field or if you only want to reset the file input of a form containing other fields, use this:
Here is an exemple: http://jsfiddle.net/v2SZJ/1/ |
|||
|
|
||||
|
Make it asynchronous, and reset it after the button's desired actions have been done.
|
|||
|
This works for me.
http://forum.jquery.com/topic/how-to-clear-a-file-input-in-ie Hope it helps. |
|||
|
Jquery is supposed to take care of the cross-browser/older browser issues for you. This works on modern browsers that I tested: Chromium v25, Firefox v20, Opera v12.14 Using jquery 1.9.1 HTML
Jquery
On jsfiddle The following javascript solution also worked for me on the browsers mention above.
On jsfiddle I have no way to test with IE, but theoretically this should work. If IE is different enough that the Javascript version does not work because MS have done it in a different way, the jquery method should in my opinion deal with it for you, else it would be worth pointing it out to the jquery team along with the method that IE requires. (I see people saying "this won't work on IE", but no vanilla javascript to show how it does work on IE (supposedly a "security feature"?), perhaps report it as a bug to MS too (if they would count it as such), so that it gets fixed in any newer release) Like mentioned in another answer, a post on the jquery forum
But jquery have now removed support for browser testing, jquery.browser. This javascript solution also worked for me, it is the vanilla equivalent of the jquery.replaceWith method.
On jsfiddle The important thing to note is that the cloneNode method does not preserve associated event handlers. See this example.
On jsfiddle But jquery.clone offers this [*1]
On jsfiddle [*1] jquery is able to do this if the events were added by jquery's methods as it keeps a copy in jquery.data, it does not work otherwise, so it's a bit of a cheat/work-around and means things are not compatible between different methods or libraries.
On jsfiddle You can not get the attached event handler direct from the element itself. Here is the general principle in vanilla javascript, this is how jquery an all other libraries do it (roughly).
On jsfiddle Of course jquery and other libraries have all the other support methods required for maintaining such a list, this is just a demonstration. |
||||
|
This works with Chrome, FF, and Safari
May not work with IE or Opera |
||||
It's easy lol (works in all browsers [except opera]):
JS Fiddle: http://jsfiddle.net/cw84x/1/ |
|||||||||
|
What? In your validation function, just put
Assuming upload is the name:
I'm using JSP, not sure if that makes a difference... Works for me, and I think it's way easier. |
|||||
|
This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.