Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically open the file picker with JavaScript? [duplicate]

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

I have naively tried the following to open the file picker programmatically with JavaScript (see fiddle here):

<input type='file'>​

<script>
    $(function () {
        $('input').click();
    });
</script>

The above doesn't work. How can I open the file picker of a input type='file' with JavaScript?

like image 341
Randomblue Avatar asked Aug 26 '12 21:08

Randomblue


1 Answers

For security reasons you can't trigger the dialog, unless it is as a response to some user triggered event. You could for instance trigger the dialog through a click on some other element:

$(function () {
    $(".someElement").click(function () {
        $('#f').click();
    });
});

Working example.

like image 50
Christofer Eliasson Avatar answered Oct 13 '22 00:10

Christofer Eliasson