2

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 ?

2 Answers 2

5

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 :)

Sign up to request clarification or add additional context in comments.

1 Comment

Should be 'uploadID' instead of '#uploadID'.
0

Using the example above...

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

JavaScript

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.