Clearing <input type='file' /> using jQuery Clearing <input type='file' /> using jQuery javascript javascript

Clearing <input type='file' /> using jQuery


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".

window.reset = function(e) {  e.wrap('<form>').closest('form').get(0).reset();  e.unwrap();}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><form>  <input id="file" type="file">  <br>  <input id="text" type="text" value="Original"></form><button onclick="reset($('#file'))">Reset file</button><button onclick="reset($('#text'))">Reset text</button>