jquery trigger: how can I trigger the browse file in the input when I click on a text link?
Asked Answered
T

4

8

Following up on this post, I have another issuer - how can I trigger the browse file in the input when I click on a text link?

Basically I want to hide the form but it will be triggered when you click on the upload text link.

<a href="#" class="upload">upload</a>
<form action="upload.php" method="post" enctype="multipart/form-data" id="myForm" style="display:none;">
  <input type="file" multiple="multiple" name="file[]" />
  <input type="submit" name="upload" value="Submit"/>
</form>
<div id="output"></div>

This is my working Javascript code:

$(document).ready(function(){
    $('.upload').click(function(){
        $(this).trigger($('input[type=file]'));
        return false;
    });

    $('input[type=file]').change(function() {
        $('#myForm').ajaxSubmit({
               target: '#output'
        });
    });
});
Thebes answered 20/8, 2011 at 15:0 Comment(1)
possible duplicate of Emulate a file upload click in jQueryThermotaxis
F
18

You can't use style="display:none;" use style="visibility:hidden;"

and I changed trigger to click:

$('.upload').click(function(){
    $('input[type=file]').click();
    return false;
});


Reasoning:

The input fields will not be sent to the server with display:none, but will be with visibility:hidden.

Fairfield answered 20/8, 2011 at 15:16 Comment(2)
The javascript worked for me to. But could you tell me why it worked and just a simple $("input[type=file]").click() wont ?Kynan
Thanks, although in my case the form element is visible. Still am unable to attach the direct "click" to it.Kynan
H
3

why don't you use a label instead? then you could use the for attribute.

<style type="text/css">
  #file_upload {
    visibility: hidden;
  }
</style>
<a href="#" class="upload">
  <label for="file_upload">upload</label
</a>
<form action="upload.php" method="post" enctype="multipart/form-data" id="myForm">
  <input id="file_upload" type="file" multiple="multiple" name="file[]" />
  <input type="submit" name="upload" value="Submit"/>
</form>
<div id="output"></div>
Hogen answered 8/10, 2015 at 14:22 Comment(0)
E
2

Joe's method is correct. However, this solution will only work in some browsers. It works on Chrome and Firefox but not on Opera, Safari, or the Android Galaxy S built-in browser (tested on current versions as of June 23, 2012). Those browsers likely disable triggering the upload button via JS for security reasons.

I will update this post if I find a solution that works in all modern browsers

Equanimous answered 25/6, 2012 at 21:50 Comment(1)
Have you found a solution yet? I have a similar issue and need to find something that works across browsers.Projectionist
B
0
<button>
  <label>
    select file
    <input type="file" style="visibility: hidden" file-handler>
  </label>
</button>

This should help you

Baltic answered 3/1, 2019 at 19:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.