Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open file browser using jquery

Tags:

jquery

I have the following code:

<p onclick="jQuery('#file').click()">Select a file</p>
<input type="file" id="file" name="file" />

When the user clicks on "Select a file", it's also supposed to open the file browser like when you press on the "Browse" button generated by <input type="file" />, where the user can select a file.

It works fine in Chrome and IE7. How do I make it work in Firefox?

like image 841
Albert Avatar asked Sep 22 '10 08:09

Albert


2 Answers

Trust me, I went to trouble already about that. It will not work on Firefox and I did not find an answer as to how to make it work.

I suspected you are trying to customize the looks of your input file. I suggest, you use opacity. Set opacity of input file to zero, to make it not visible. Then put above it two span element above it. Then you could style the span as much as you like. When you click any of the span, the click on input file is also trigger.

simple demo


added notes:

With that you have the power to change the look of input file. The problem is, opacity won't work on IE6. ;)

like image 71
Reigel Avatar answered Nov 15 '22 16:11

Reigel


There is a better way to open the dialog (it works across all modern browsers at this time). Just change

jQuery('#file').click();

to

jQuery('#file').trigger('click');
like image 38
Jason Avatar answered Nov 15 '22 18:11

Jason