Am trying to trigger an upload box (browse button) using jQuery.
The method I have tried now is:
$('#fileinput').trigger('click');
But it doesn't seem to work. Please help. Thank you.
Am trying to trigger an upload box (browse button) using jQuery.
The method I have tried now is:
$('#fileinput').trigger('click');
But it doesn't seem to work. Please help. Thank you.
Correct code:
This is a very old question, but unfortunately this issue is still relevant and requires a solution.
The (suprisingly simple) solution I've come up with is to "hide" the actual file input field and wrap it with a LABEL tag (can be based on Bootstrap and HTML5, for enhancement).
See here:
Example code hereThis way, the real file input field is invisible and all you see is the customized "button" which is actually a modified LABEL element. When you click on this LABEL element, the window for selecting a file comes up and the file you choose will go into the real file input field.
On top of that, you can manipulate the look and behaviour as you wish (for example: get the name of the selected file from the file input file, after selected, and show it somewhere. The LABEL element doesn't do that automatically, of course. I usually just put it inside the LABEL, as its text content).
Beware though! The manipulation of the look and behaviour of this is limited to whatever you can imagine and think of. ;-) ;-)
this worked for me:
JS:
HTML:
CSS:
>>>Another one that works Cross-Browser:<<<
The Idea is that you overlay an invisible huge "Browse" button over your custom button. So when the user clicks your custom button, he's actually clicking on the "Browse" button of the native input field.
JS Fiddle: http://jsfiddle.net/5Rh7b/
HTML:
CSS:
JavaScript:
It is too late to answer but I think this minimal setup work best. I am also looking for the same.
//css
jsbin
bootstrap file input buttons demo
i had problems with custom client side validation for
<input type="file"/>
while using a fake button to trigger it and @Guillaume Bodi's solution worked for me (also withopacity: 0;
on chrome)and css style for upload input
My problem was a little bit different on iOS 7. Turns out FastClick was causing problems. All I had to do was add
class="needsclick"
to my button.