Categories
JavaScript Answers

How to fire event on file select with JavaScript?

Spread the love

Sometimes, we want to fire event on file select with JavaScript.

In this article, we’ll look at how to fire event on file select with JavaScript.

How to fire event on file select with JavaScript?

To fire event on file select with JavaScript, we can listen to the file input’s change event.

For instance, we write

<input type="file" id="file" name="file" />

to add a file input.

Then we write

document.getElementById("file").onchange = () => {
  // ...
};

to select the file input with getElementById.

And then we set its onchange property to a function that runs when file selection changes.

Conclusion

To fire event on file select with JavaScript, we can listen to the file input’s change event.

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 *