How to clear an HTML file upload field via JavaScript?

Sometimes, we want to clear an HTML file upload field via JavaScript.

In this article, we’ll look at how to clear an HTML file upload field via JavaScript.

How to clear an HTML file upload field via JavaScript?

To clear an HTML file upload field via JavaScript, we set the file input’s value property to an empty string.

For instance, we write

<form>
  <input id="fileInput" name="fileInput" type="file" />
</form>

to add a form with a file input.

Then we write

document.getElementById("fileInput").value = "";

to select the file input with getElementById.

And we set its value property to an empty string.

Conclusion

To clear an HTML file upload field via JavaScript, we set the file input’s value property to an empty string.