Categories
JavaScript Answers

How to get file name when user select a file from the file input with JavaScript?

Spread the love

Sometimes, we want to get file name when user select a file from the file input with JavaScript.

In this article, we’ll look at how to get file name when user select a file from the file input with JavaScript.

How to get file name when user select a file from the file input with JavaScript?

To get file name when user select a file from the file input with JavaScript.

For instance, we write

document.getElementById("fileInput").onchange = (e) => {
  console.log(e.target.value);
};

to select the file input with getElementById.

Then we set its onchange property to a function that get the selected file’s name with e.target.value.

Conclusion

To get file name when user select a file from the file input with JavaScript.

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 *