Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safari input='file' doesn't work

I have div which one on click trigger input-file. In other browsers (Chrome, Firefox, IE) it's working but in Safari(5.1.7) it doesn't. Any idea why?

 $(function() {
 $("#mask-button-file").click(function () {
    $("#mask-load-file").trigger('click');
    });
});


<div id="mask-button-file" class="hover active">
        . . .
</div>
<input type="file" id="mask-load-file" name="file1" >

#mask-load-file{
display:none;
opacity:0;
}
#mask-button-file{
width:81px;
height: 40px;
background-color: #f70808;
float:left;
text-align: center;
vertical-align: middle;
line-height: 40px;
margin-left:1px;
cursor:pointer;
}
#mask-button-file:active{
background-color:#DF0005;
}
like image 531
snuuve Avatar asked Jul 27 '14 11:07

snuuve


1 Answers

It's because the input is being set to display:none and this has issue in safari. Instead of hiding it, wrap it inside a div with height, width set to zero.

<div>
  <input type="file" id="myfile" name="upload"/>
</div>

CSS:

div {
     width: 0px;
     height: 0px;
     overflow: hidden;
    }

Another option will be to position the input out of the viewport so that it's not visible.

You can refer this post: Jquery trigger file input for more options about it.

like image 178
K K Avatar answered Nov 03 '22 21:11

K K