Categories
JavaScript Answers

How to clear an HTML file upload field via JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *