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


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

I’d like to make a click event fire on an tag programmatically.
Just calling click() doesn’t seem to do anything or at least it doesn’t pop up a file selection dialog.
I’ve been experimenting with capturing events using listeners and redirecting the event, but I haven’t been able to get that to actually perform the event like someone clicked on it.


Solution 1:

You cannot do that in all browsers, supposedly IE does allow it, but Mozilla and Opera do not.

When you compose a message in GMail, the ‘attach files’ feature is implemented one way for IE and any browser that supports this, and then implemented another way for Firefox and those browsers that do not.

I don’t know why you cannot do it, but one thing that is a security risk, and which you are not allowed to do in any browser, is programmatically set the file name on the HTML File element.

Solution 2:

I have been searching for solution to this whole day. And these are the conclusions that I have made:

  1. For the security reasons Opera and Firefox don’t allow to trigger file input.
  2. The only convenient alternative is to create a “hidden” file input (using opacity, not “hidden” or “display: none”!) and afterwards create the button “below” it. In this way the button is seen but on user click it actually activates the file input.

Hope this helps! 🙂

<div style="display: block; width: 100px; height: 20px; overflow: hidden;">
<button style="width: 110px; height: 30px; position: relative; top: -5px; left: -5px;"><a href="javascript: void(0)">Upload File</a></button>
<input type="file" id="upload_input" name="upload" style="font-size: 50px; width: 120px; opacity: 0; filter:alpha(opacity=0);  position: relative; top: -40px;; left: -20px" />

Solution 3:

You can fire click() on any browser but some browsers need the element to be visible and focused. Here’s a jQuery example:


It works with the hide before the click() but I don’t know if it works without calling the show method. Never tried this on Opera, I tested on IE/FF/Safari/Chrome and it works. I hope this will help.

Solution 4:

Under FF4+, Opera ?, Chrome:

  1. should be called from user action context! (not script execution context)

  2. <input type="file" /> should be visible ( !== 'none') (you can hide it with visibility or something other, but not “display” property)

Solution 5:

For those who understand that you have to overlay an invisible form over the link, but are too lazy to write, I wrote it for you. Well, for me, but might as well share. Comments are welcome.

HTML (Somewhere):

<a id="fileLink" href="javascript:fileBrowse();" onmouseover="fileMove();">File Browse</a>

HTML (Somewhere you don’t care about):

<div id="uploadForm" style="filter:alpha(opacity=0); opacity: 0.0; width: 300px; cursor: pointer;">
    <form method="POST" enctype="multipart/form-data">
        <input type="file" name="file" />


function pageY(el) {
    var ot = 0;
    while (el && el.offsetParent != el) {
        ot += el.offsetTop ? el.offsetTop : 0;
        el = el.offsetParent;
    return ot;

function pageX(el) {
    var ol = 0;
    while (el && el.offsetParent != el) {
        ol += el.offsetLeft ? el.offsetLeft : 0;
        el = el.offsetParent;
    return ol;

function fileMove() {
    if (navigator.appName == "Microsoft Internet Explorer") {
        return; // Don't need to do this in IE. 
    var link = document.getElementById("fileLink");
    var form = document.getElementById("uploadForm");
    var x = pageX(link);
    var y = pageY(link); = 'absolute'; = x + 'px'; = y + 'px';

function fileBrowse() {
    // This works in IE only. Doesn't do jack in FF. :( 
    var browseField = document.getElementById("uploadForm").file;;

Solution 6:

Try this solution: