Am trying to trigger an upload box (browse button) using jQuery.
The method I have tried now is:
$(\'#fileinput\').trigger(\'click\');
You can click the input file from your JQuery while keeping it hidden fully.
I am using this:
< input type="file" name="article_input_file" id="article_input_file" accept=".xlsx,.xls" style="display: none" >
$("#article_input_file").click();
this works from within any standard script tag in your HTML page.
This is due to a security restriction.
I found out that the security restriction is only when the <input type="file"/>
is set to display:none;
or is visbilty:hidden
.
So i tried positioning it outside the viewport by setting position:absolute
and top:-100px;
and voilà it works.
see http://jsfiddle.net/DSARd/1/
call it a hack.
Hope that works for you.
I think i understand your problem, because i have a similar.
So the tag <label>
have the atribute for, you can use this atribute to link your input with type="file".
But if you don't want to activate this using this label because some rule of your layout, you can do like this.
$(document).ready(function(){
var reference = $(document).find("#main");
reference.find(".js-btn-upload").attr({
formenctype: 'multipart/form-data'
});
reference.find(".js-btn-upload").click(function(){
reference.find("label").trigger("click");
});
});
.hide{
overflow: hidden;
visibility: hidden;
/*Style for hide the elements, don't put the element "out" of the screen*/
}
.btn{
/*button style*/
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<div id="main">
<form enctype"multipart/formdata" id="form-id" class="hide" method="post" action="your-action">
<label for="input-id" class="hide"></label>
<input type="file" id="input-id" class="hide"/>
</form>
<button class="btn js-btn-upload">click me</button>
</div>
Of course you will adapt this for your own purpose and layout, but that's the more easily way i know to make it work!!
Actually, I found out a really easy method for this, which is:
$('#fileinput').show().trigger('click').hide();
This way, your file input field can have the css property display on none and still win the trade :)
this worked for me:
JS:
$('#fileinput').trigger('click');
HTML:
<div class="hiddenfile">
<input name="upload" type="file" id="fileinput"/>
</div>
CSS:
.hiddenfile {
width: 0px;
height: 0px;
overflow: hidden;
}
>>>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:
<div id="mybutton">
<input type="file" id="myfile" name="upload"/>
Click Me!
</div>
CSS:
div#mybutton {
/* IMPORTANT STUFF */
overflow: hidden;
position: relative;
/* SOME STYLING */
width: 50px;
height: 28px;
border: 1px solid green;
font-weight: bold
background: red;
}
div#mybutton:hover {
background: green;
}
input#myfile {
height: 30px;
cursor: pointer;
position: absolute;
top: 0px;
right: 0px;
font-size: 100px;
z-index: 2;
opacity: 0.0; /* Standard: FF gt 1.5, Opera, Safari */
filter: alpha(opacity=0); /* IE lt 8 */
-ms-filter: "alpha(opacity=0)"; /* IE 8 */
-khtml-opacity: 0.0; /* Safari 1.x */
-moz-opacity: 0.0; /* FF lt 1.5, Netscape */
}
JavaScript:
$(document).ready(function() {
$('#myfile').change(function(evt) {
alert($(this).val());
});
});
I have it working (=tested) in IE8+, recent FF and chrome:
$('#uploadInput').focus().trigger('click');
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 not visibility:hidden
).
I suggest (as many other have before) to absolutely position the input and throw it off screen.
#uploadInput {
position: absolute;
left: -9999px;
}