Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open select file dialog via js?

$('#id').click(); 

It doesn't work on Chrome 26 on Mac OS.

The problem actually is creation "upload" widget that can be integrated in a form. Widget will consists of two parts. The first part is div with initiator button and error/success messages. I think the way is put another form as the second part with file input and submit file into the iframe. After submition we fill hidden field in first part in main form or show errors in the same.

Easy way is adding file-form into main-form, but it's prohibited.

like image 490
Luciuz Avatar asked Apr 25 '13 13:04

Luciuz


People also ask

How to open file dialog JavaScript?

Use the type="file" Attribute in HTML and onchange Method in JavaScript to Open File Dialog. We will create a button element in the following instance, and an onclick attribute will follow this. This will trigger a function in the JavaScript file.

How do I open an input file?

If you cannot open your INPUT file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a INPUT file directly in the browser: Just drag the file onto this browser window and drop it.


1 Answers

Using jQuery

I would create a button and an invisible input like so:

<button id="button">Open</button> <input id="file-input" type="file" name="name" style="display: none;" /> 

and add some jQuery to trigger it:

$('#button').on('click', function() {     $('#file-input').trigger('click'); }); 

Using Vanilla JS

Same idea, without jQuery (credits to @Pascale):

<button onclick="document.getElementById('file-input').click();">Open</button> <input id="file-input" type="file" name="name" style="display: none;" /> 
like image 94
Ron van der Heijden Avatar answered Sep 25 '22 14:09

Ron van der Heijden