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.
This is due to a security restriction.
I found out that the security restriction is only when the
<input type="file"/>
is set todisplay:none;
or isvisbilty:hidden
.So i tried positioning it outside the viewport by setting
position:absolute
andtop:-100px;
and voilà it works.see http://jsfiddle.net/DSARd/1/
call it a hack.
Hope that works for you.
I managed with a simple $(...).click(); with JQuery 1.6.1
This is probably the best answer, keeping in mind the cross browser issues.
CSS:
JS:
HTML:
Check out my fiddle.
http://jsfiddle.net/mohany2712/vaw8k/
I have it working (=tested) in IE8+, recent FF and chrome:
The key is focusing before firing the click (otherwise chrome ignores it).
Note: you do NEED to have your input displayed and visible (as in, not
display:none
and notvisibility:hidden
). I suggest (as many other have before) to absolutely position the input and throw it off screen.or else simply