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

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.