How to open the select file dialog via JavaScript?

Spread the love

To open the select file dialog via JavaScript, we call the file input’s click method.

For instance, we write

<button onclick="document.getElementById('file-input').click();">Open</button>
<input id="file-input" type="file" name="name" style="display: none" />

to add a button and set its onclick attribute to select the file input with getElementById on click.

And we call click to open the select file dialog.

We add a file input and hide it with display: none.

Leave a Reply

Your email address will not be published. Required fields are marked *