Detect when a user chooses a file using a file input

Pablo picture Pablo · Jul 10, 2010 · Viewed 13.5k times · Source

How can I detect when a user has chosen which file to upload using a html file input. That way I can submit the form automatically.

Answer

Faisal picture Faisal · Jul 10, 2010

The file upload element fires an onchange event when its contents have changed. Here's a very terse example:

<input type="file" name="whatever" onchange="alert('Changed!');" />

EDIT: Oh, and if you'd like to submit the form when they select something (although this will probably be a little confusing to your users):

<input type="file" name="whatever" onchange="this.form.submit();" />