Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>An HTML file input can't be cleared by normal means (i.e. setting the value to null or an empty string), but because of browser security restrictions, most browsers don't allow this. In many browsers, setting a null value attribute either will have no effect or cause an error. Instead, create a new element that clones the old element and swap the two. </p> <p>Read : <a href="http://aspnetupload.com/Clear-HTML-File-Input.aspx" rel="nofollow">How to clear HTML file inputs</a></p> <hr> <blockquote> <p>Trick:- You have to create new input type file and replace it with old one.</p> </blockquote> <hr> <p><strong>js</strong></p> <pre><code>function clearFileInput() { var oldInput = document.getElementById("files1"); var newInput = document.createElement("input"); newInput.type = "file"; newInput.id = oldInput.id; newInput.name = oldInput.name; newInput.className = oldInput.className; newInput.style.cssText = oldInput.style.cssText; // copy any other relevant attributes oldInput.parentNode.replaceChild(newInput, oldInput); } </code></pre> <p><kbd><a href="http://jsfiddle.net/cse_tushar/7EVS4/" rel="nofollow"><strong>fiddle Demo</strong></a></kbd> <hr> Updated after OP's comment</p> <p><kbd><a href="http://jsfiddle.net/cse_tushar/7EVS4/2" rel="nofollow"><strong>Updated fiddle Demo</strong></a></kbd></p> <pre><code>$(document).ready(function () { function clearFileInput(el) { var oldInput = document.getElementById(el); var newInput = document.createElement("input"); newInput.type = "file"; newInput.id = oldInput.id; newInput.name = oldInput.name; newInput.className = oldInput.className; newInput.style.cssText = oldInput.style.cssText; // copy any other relevant attributes oldInput.parentNode.replaceChild(newInput, oldInput); } $('.reset').click(function () { clearFileInput($(this).prev('input[type="file"]').attr('id')); }); }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload