How do I submit a "file" input without submit button using JavaScript?

Pennywise83 picture Pennywise83 · Dec 15, 2009 · Viewed 69.3k times · Source

There is a way to automatically submit a form without clicking to a "submit" button?

I have a form with one "file" input. I would submit the form after the user have selected one file.

Answer

Marius picture Marius · Dec 15, 2009

yes, you can use the form.submit() function. Add an onchange listener on the file input and link it to the form.submit() function, like so:

<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" onchange="this.form.submit()" name="myFile"/>
</form>