Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when a user chooses a file using a file input

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.

like image 228
Pablo Avatar asked Jul 10 '10 10:07

Pablo


2 Answers

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();" />
like image 139
Faisal Avatar answered Oct 05 '22 14:10

Faisal


try

$('#inputfileID').change(function(){
alert(0);
})​
like image 42
Reigel Avatar answered Oct 05 '22 15:10

Reigel