Categories
JavaScript Answers

How to determine if user selected a file for file upload with JavaScript?

Spread the love

Sometimes, we want to determine if user selected a file for file upload with JavaScript.

In this article, we’ll look at how to determine if user selected a file for file upload with JavaScript.

How to determine if user selected a file for file upload with JavaScript?

To determine if user selected a file for file upload with JavaScript, we check if the value property is not an empty string.

For instance, we write

if (document.getElementById("uploadBox").value !== "") {
  // ...
}

to select the file input with getElementById.

And if its value isn’t an empty string, then a file is selected.

Conclusion

To determine if user selected a file for file upload with JavaScript, we check if the value property is not 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 *