How to get a file’s name when the user selects a file via a file input with JavaScript?

Spread the love

To get a file’s name when the user selects a file via a file input with JavaScript, we use the value property.

For instance, we write

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

to select the file input with getElementById.

And we set its onchange property to a function that gets trhe selected file’s name with e.target.value.

onchange is called when file selection changes.

Leave a Reply

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