13

i have this mark up

<div id="wrapper"> <input type="file" id="file" accept="image/*" capture="camera"> <a href="#" id="capture">Submit Cleanup</a> </div> 

and jQuery as follow

jQuery(function($){ $('#capture').on('click', function(e){ e.preventDefault(); $('#file').trigger('click'); }); }); 

the script works as expected on PC browser, but when i try on mobile device the camera doesnt prompt. also i already tried using click(), but same result.

what could be the problem?

6
  • Have you tried $('#file')[0].trigger('click');? Commented Sep 17, 2014 at 9:11
  • @Satpal yes, already tried that as well, still not working. Anton could it be the problem comes from the phone it self? :| Commented Sep 17, 2014 at 9:13
  • Works fine on my android v4.4.2 jsfiddle.net/qd2r01er What phone are you trying on? Commented Sep 17, 2014 at 9:14
  • $('#file').trigger('click'); Commented Sep 17, 2014 at 9:14
  • 1
    @JqueryKing please see my current code.. Commented Sep 17, 2014 at 9:16

3 Answers 3

18

It's a security feature. Some browsers don't allow a non-manual click on file inputs. You can read more about it here and here.

Why isn't it possible to programmatically trigger the file input selection?

Most browsers prevent submitting files when the input field didn't receive a direct click (or keyboard) event as a security precaution. Some browsers (e.g. Google Chrome) simply prevent the click event, while e.g. Internet Explorer doesn't submit any files that have been selected by a programmatically triggered file input field. Firefox 4 (and later) is so far the only browser with full support for invoking "click"-Events on a completely hidden (display: none) file input field.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the question. i'll make changes as explained
10

In most browsers(

jQuery(function($){ $('#capture').on('click', function(e){ e.preventDefault(); $('#file')[0].click(); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="wrapper"> <input type="file" id="file" accept="image/*" capture="camera"> <a href="#" id="capture">Submit Cleanup</a> </div>

Comments

8

<label> <input type="file" name="myfile" accept="application/pdf" id="myfile" style="display:none"> CLICK HERE! </label> <br> <a href="javascript:document.querySelector('input#myfile').click()">OR HERE</a>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.