2

I have the following situation:

<input id="upload_trigger_btn" type="file" name="files[]" multiple> <a type="submit" class="button-normal-12px-ffffff" href="#"><span>Add files</span></a> $(".add_files_btn").bind("click", function () { $("#upload_trigger_btn").trigger("click"); }); 

So basically i want to trigger the file input button using another. This works great in firefox and IE but the button doesn't get triggered in chrome and safari.

2
  • 1
    The last time I tried to accomplish this, I found that it isn't possible to manually trigger that dialog in chrome and safari. It's been a while since then, so maybe someone else has come up with a solution to it. Commented Jan 23, 2012 at 19:39
  • The last time i tried it, i ended up making the buttons invisible and placing them on top of the button that needed to trigger it so that it would look like you were clicking on your fancy button, but you were actually clicking on the file input. Commented Jan 23, 2012 at 20:02

4 Answers 4

4

You can make {'visibility':'hidden'} with height: 0px and display block, trigger would work with it.

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

Comments

1

I think you could get away with:

$(".add_files_btn").unbind("click").bind("click", function () { $("#upload_trigger_btn").click(); }); 

3 Comments

is 'upload_trigger_btn' control visible?
And that seemed to be the problem, changed from display:none to visibility:hidden and now it gets triggered.
Oh sorry, didnt know you war the original poster of the answer, but its definitly accepted, and ty very much for your help
1

EDIT

In most cases you can trigger an event like this:

$(".add_files_btn").click(function(){ $("#upload_trigger_btn").click(); }); 

But when it comes to file upload buttons, a few browsers (including firefox) do not allow the action because it poses a security risk. Unfortunately, there does not appear to be a simple fix with a click function...

Reference -> https://stackoverflow.com/a/3030174/992435

2 Comments

Apparently it is not possible with file fields. As I understand it, it is a security risk and a couple of browsers do not allow it. The code I posted originally DID work in chrome, but not in FF.
It does seem to be possible, but you cant use display:none on the file field if you want to trigger it. When you use visibility:hidden it does work.
-2
 <div class="btn btn-default fileinput-button "> <span><i class="fa fa-cloud-upload pr-10" aria-hidden="true"></i>UPLOAD FILE</span> <input id="fileupload5" type="file"> </div> 

.fileinput-button input { position: absolute; top: 0; right: 0; margin: 0; opacity: 0; -ms-filter: 'alpha(opacity=0)'; font-size: 200px; cursor: pointer; }

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.