brodo
brodo

Reputation: 65

Call function on file upload

I have a form which I'm using to upload files. In current situation if a user choose an image from his computer he have to click button upload to upload the image. I'm trying to find a way to skip the step with a button pressing.

How to call a javascript function when the file is selected from user ?

Upvotes: 2

Views: 12658

Answers (2)

PJately
PJately

Reputation: 477

Using the example above...

<input type="file" name="someName" id="uploadID" />

JavaScript

document.getElementById('uploadID').addEventListener('change', () => {
//Your code...
});

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382909

The onchange event is fired when a user specify a file for the upload filed. You could go about something like this:

<input type="file" name="someName" id="uploadID" />

Javascript:

var el = document.getElementById('#uploadID');
el.onchange = function(){
  // your code...
};

However, javascript validation is good idea but make sure that you do the actual validation on the server-side :)

Upvotes: 5

Related Questions