13

I am trying to trigger a click event on input type="file" on the click of an another button.

Demo: http://jsfiddle.net/Y85g6/

It's working fine in all browsers except on safari browsers in mac, Ipad & Iphone.

Is there any trick to accomplish this task?

Mandeep Pasbola
  • 2,631
  • 2
  • 26
  • 50
  • possible duplicate of [In JavaScript can I make a "click" event fire programmatically for a file input element?](http://stackoverflow.com/questions/210643/in-javascript-can-i-make-a-click-event-fire-programmatically-for-a-file-input) – ceejayoz Jan 19 '12 at 15:12
  • then how to make a customized file browse functionality. – Mandeep Pasbola Jan 19 '12 at 15:22
  • Either don't, or make it with a Flash-based solution like SWFUpload. – ceejayoz Jan 19 '12 at 15:22

11 Answers11

14

Instead of trigger("click") you can use the following code which works successfully in all browsers:

var evt = document.createEvent("HTMLEvents");
evt.initEvent("click", true, true); 
document.getElementById(elem_id).dispatchEvent(evt);
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Sattu
  • 139
  • 1
  • 8
  • Only `initEvent()` is now deprecated. The `jQuery.trigger("click")` has the advantage of using the newest available interface as made available... – Alexis Wilke Mar 18 '16 at 01:47
6

Found an alternative.

Just position the input type="file" over the custom button by absolute positioning it and use jQuery fadeTo('fast',0) to hide it.

Now if we click over the custom button file browser window appears.

Its working in all desktop browsers but not in iPhone & iPad as they don't allow to upload any file.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Mandeep Pasbola
  • 2,631
  • 2
  • 26
  • 50
5

make the element visible, as trigger event doesnt work on hidden elements in mac safari.

rajansoft1
  • 1,336
  • 2
  • 18
  • 38
2

The following approach did the trick for me:

$(".upload_on_click").each(function(index) {
    var form = $(this).next("form");
    //form.hide(); /* THIS TECHNIQUE DIDN'T WORK IN SAFARI */
    form.css("position", "absolute");
    form.css("visibility", "hidden");
    $(this).css("cursor", "pointer");
    $(this).click(function() {
        $('input[type="file"]', form).trigger("click");
    });
    $('input[type="file"]', form).change(function() {
        $('input[type="submit"]', form).trigger("click");
    });
});
John Erck
  • 9,478
  • 8
  • 61
  • 71
1

Not sure if anybody is reading this question anymore, but I recently had a similar issue with Safari, so I thought I'd share.

First, as ceejayoz mentioned, see the discussion here: In JavaScript can I make a "click" event fire programmatically for a file input element?

The solution, then, if you do not want to use button positioning, is to display the file input button (remove the "display:none;") and instead hide it using "opacity:0;". You probably also want to absolute position it so it doesn't interact with other elements. Anyway, this way you can still use JS to activate the file input in all browsers.

Community
  • 1
  • 1
Sam Grondahl
  • 2,397
  • 2
  • 20
  • 26
1

.click() is not supported in Safari. Sattu's suggestion should work. But you don't need Javascript for customizing input file button.

<label><input type="file" id="file" name="file" style="position:absolute; left:-9999px;" />Upload</label>

Reference: http://noypiscripter.blogspot.com/2014/04/simplest-cross-browser-custom-upload.html

noypiscripter
  • 1,451
  • 1
  • 13
  • 13
1

Browsers can be very finicky when it comes to JavaScript interactions with file inputs, for security reasons. Safari prevents you from firing any click events on them. Some versions of Chrome and Firefox also have this restriction. This has been previously discussed here.

Community
  • 1
  • 1
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
1

Its better to use CSS instead of JS to hide element because it will render your element as hidden directly.

Trython
  • 101
  • 1
  • 2
0

I got the simplest answer for this

opacity : 0.01 on your input file element

eighteyes
  • 1,306
  • 1
  • 11
  • 18
0

simply remove "display:none" and use "visibility:hidden" and works cross browser.

-1

Try loading your script in the footer. I had a similar problem a few times and that did the trick.

Susan
  • 11