Alexander Shmatko Alexander Shmatko - 4 months ago 21
HTML Question

How to determine if user selected a file for file upload in real time?

Suppose, I have an input

<input id="uploadFile" type="file" />


In the real time, I need to output an alert if a file was selected for this input. If a file wasn't selected and user simply dismissed files manager window I don't need to output an alert.

How can I do this?

Answer

You can use the change event to do this. Under that event you can check the value of the file input. If it's empty, no file was chosen.

$('#uploadFile').change(function() {
    if (this.value) {
        alert('you chose a file!');
    }
})

Working example