Sometimes, we want to auto-submit an upload form when a file is selected with JavaScript.
In this article, we’ll look at how to auto-submit an upload form when a file is selected with JavaScript.
How to auto-submit an upload form when a file is selected with JavaScript?
To auto-submit an upload form when a file is selected with JavaScript, we call submit
on the form when a file is selected.
For instance, we write
document.getElementById("file").onchange = () => {
document.getElementById("form").submit();
};
to select the file input with document.getElementById("file")
.
Then we set its onchange
property to a function that calls submit
to submit the form`.
Conclusion
To auto-submit an upload form when a file is selected with JavaScript, we call submit
on the form when a file is selected.