Wednesday, January 28, 2015

In JavaScript can I make a “click” event fire programmatically for a file input element?

Problem:

I'd like to make a click event fire on an <input type="file"> tag programmatically. That means one function will call when I finished upload a file.
or
How can I make a click event programmatically after upload a file with file input element?

Solution:

You can simply use "onchange" attribute in input element. You write a function in javascript and call it with "onchange"  attribute that will work like onclick.

Example:
<html>
<body>
<div>
    <input id="myUpload" type="file" onchange="afterCall();" autofocus> </input>
</div>

<script type='text/javascript'>
function afterCall(){
        alert("Thank you for upload a file");
}
</script>

</body>
</html>

Suggestion:

If you have better solution for this question please don't hesitate to give a comment bellow.
Thank you.