Reputation: 3461
is there a way to automatically call a function using an event after the user has browsed a file using a
<input type="file">
I've been trying to find something for the past hour but failed horribly, I saw that I should try onchange but that also failed..
Upvotes: 1
Views: 62
Reputation: 31300
There is a way:
<input type="file" onchange="myFunction()">
The onchange
is simple. So, I'm guessing that the code that is getting triggered is failing. You need to be able to retrieve the file from the INPUT object. To pass the file as an argument on the function call, you can use something like this:
<form class='frmUpload'>
<input name="picOneUpload" type="file" accept="image/*" onchange="picUpload(this.parentNode)" >
</form>
If you do it that way, the arg passed with be an object from the FORM, then you need to get the object of the INPUT that is inside of the FORM object.
If you want to avoid that, you can use something like this:
<input name="picOneUpload" type="file" accept="image/*" onchange="picUpload(this.files[0])" >
That passes the first file from the INPUT object to the picUpload
function.
Upvotes: 1