I have this small problem with a website I’m designing. It’s fairly over, but I think the jQuery is the problem here.
The code is as follows:
<img src="files/register_page/upload_photo/body.png" id="upload">
<input type="file" name="file_upload" id="file_upload" style="visibility:hidden;">
And the jQuery is the following, in the part of the document:
$(document).ready( function(){
$("#upload").click( function(){
$('#file_upload').click();
});
});
However, upon clicking the img, nothing happens, where a file upload dialog should pop up.
Please, if you need any more information or something, let me know. If not, what could the problem be here? I’m using jQuery 1.8.0.
Using HTML5 label element:
Fiddle
Works in Chrome, IE and Opera but not FF.
Browsers tend to limit what you can do with
input type="file". The label element redirects the focus to the input with the id property equal to itsforproperty, so this is an workaround to trigger the invisible element.Modern browsers have alleviated a good part of those restrictions though, so you can trigger the
.click()without problem.You may however, change the CSS hack to hide it without using
display:noneorvisibility:hiddenfor back-compat with older browsers. EitherOr
Can hide the element without using the
displayorvisibilityCSS properties.Fiddle
Also, here’s an workaround for the label element on FF, you can change the
visibility:hiddenbyopacity:0and:Fiddle. But this should be unnecessary if the primary script works fine.