Pascal Klein
Pascal Klein

Reputation: 24853

HTML <input type="file"> how to get filename right after file was chosen

This is my input tag:

<input id="myId" class="button" name="myFile" type="file">

As soon as the user chooses a file, I would like to get the name of the file, to display it and give the user some feedback. I know that I can get the filename in Javascript (using Jquery) like so:

var fileName = $('#myId').val();

However, I would like to do a function call as soon, as the user picks a file. If a add an onclick-even at the "myId" Tag, this event will get called as soon as the user clicks the button, but BEFORE he picks a file. What event is being triggered when the user chooses a file? How can I call a function right after the user picks a file?

Upvotes: 2

Views: 5437

Answers (1)

jd.
jd.

Reputation: 4098

Since you are already using jQuery, the solution is quite straight forward:

$("#myId").change(function(){
   doIt();
 });

Upvotes: 4

Related Questions