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

things you can do with javascript on a website
fun things to do with javascript
javascript projects for learning
w3schools javascript
javascript function
100 javascript projects
javascript project tutorial
javascript projects for resume

I'd like to make a click event fire on an <input type="file"> 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.

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.

Here Are 10 Projects You Can Do To Build Your JavaScript Skills , Having a hard time visualizing exactly where JavaScript fits in the web development scheme? Try getting hands-on with these 10 JavaScript project ideas in  How To Repair Free Javascript & Protect Your PC By MajorAV. Free Download Now! Scan & Resolve Issues In 3 Mins, 100% Guaranteed. Download & Secure Your PC Now.

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" />
</div>

JavaScript Functions, You will learn a lot more about function invocation later in this tutorial. Function Return. When JavaScript reaches a return statement, the function will stop  Javascript is a general-purpose language which has found widespread use in making web pages responsive and dynamic. It’s used for things like error-checking input fields in real time or writing a personalized greeting based on the time of the day.

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

$('#input_element').show();
$('#input_element').focus();
$('#input_element').click();
$('#input_element').hide();

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.

JavaScript Objects, Object Definition. You define (and create) a JavaScript object with an object literal: Do Not Declare Strings, Numbers, and Booleans as Objects! When a  Botpress is a framework for creating and managing chatbots using javascript. Plus, just about all the chatbot-as-a-service products use standard web protocols, which means javascript works perfectly.

THIS IS POSSIBLE: Under FF4+, Opera ?, Chrome: but:

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

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

JavaScript Scope, Do NOT create global variables unless you intend to. Your global variables (or functions) can overwrite window variables (or functions). Any function, including  Many Internet Web sites contain JavaScript, a scripting programming language that runs on the web browser to make specific features on the web page functional. If JavaScript has been disabled within your browser, the content or the functionality of the web page can be limited or unavailable.

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" />
    </form>
</div>

JavaScript:

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);
    form.style.position = 'absolute';
    form.style.left = x + 'px';
    form.style.top = y + 'px';
}

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

JavaScript Function Definitions, function keyword. You can use a function declaration or a function expression. Functions stored in variables do not need function names. They are always  I would like to make a button on a page that can call a JS function in the same page. The function will need to create (open) new window which its HTML code was given from the JS function itself. How can I do that? The purpose of this is to produce a print friendly page out of a specific page. Please notice: No AJAX can be used.

JavaScript Where To, For example, a function can be called when an event occurs, like when the user clicks a button. You will learn much more about functions and events in later  How to enable JavaScript in your browser Nowadays almost all web pages contain JavaScript, a scripting programming language that runs on visitor's web browser. It makes web pages functional for specific purposes and if disabled for some reason, the content or the functionality of the web page can be limited or unavailable.

An Introduction to JavaScript, What can in-browser JavaScript do? Modern JavaScript is a “safe” programming language. It  Loops can execute a block of code a number of times. Loops are handy, if you want to run the same code over and over again, each time with a different value. The for loop has the following syntax: Statement 1 is executed (one time) before the execution of the code block. Statement 2 defines the condition for executing the code block.

Object.create(), create , this polyfill cannot support it due to a limitation inherent in versions of ECMAScript lower than 5. if (typeof Object.create !== "function") {  Normally, JavaScript strings are primitive values, created from literals: var firstName = "John"; But strings can also be defined as objects with the keyword new: var firstName = new String ("John"); Example. var x = "John"; var y = new String ("John"); // typeof x will return string. // typeof y will return object.

Comments
  • The click event you trigger must be called within a click event started by the user, or it won't work.
  • Drat. Well I certainly understand that it's exploitable. Is this documented anywhere? I guess it would be implemented by each browser?
  • Updated my answer to be more correct than previous answer - the gist is the same, but clarification should help a bit. This guy ran into the same problem: bytes.com/forum/thread542877.html
  • Thanks God FF5 allows this click
  • To clarify above comment: Chrome recently changed to check whether the file input element is visible. Triggering the click method works, including in the console, if the element can be seen.
  • @Otvazhnii - dude, this answer (the one you're saying is wrong) is 10 years old - no surprise it's not correct! (I don't know for sure, I'm taking your word for it). :P
  • this solution works great. Not sure why it's been overlooked and un-uprated. It's not *exactly what the question asks for, but it's a great work around. Have you found it to be incompatible with any browsers? I don't have the time to work my way through all 10+ flavors of relevant ones to test on.
  • Thank you for this answer, it's awesome :D
  • Nice solution. Works on android mobile browser too.
  • Thanks for that! We used similar approach but not working correcly in IE... Outer div overflow: hidden; and input font-size: 50px; styles make it working for me!