The code below works everywhere except on safari mobile. Apparently the onchange is never triggered.
JavaScript
x
14
14
1
// create a hidden file input element
2
var input = document.createElement("input");
3
input.type = "file";
4
5
// when the input content changes, do something
6
input.onchange =
7
function(event)
8
{
9
// upload files
10
}
11
12
// Trigger file browser
13
input.click();
14
I have found similar examples however they all refer to scenarios where there is even a form of some other visible representation of the file input and they all involve form-clearing workarounds. That wouldn’t work here.
This code is being called upon clicking a picture, in order to upload a new one as a replacement.
Any hints? Anything I’m doing wrong?
Advertisement
Answer
I’ll be damned: on iOS safari two extra conditions are necessary compared to other browsers:
1) The input must be actually appended to the DOM.
2) setting .onchange won’t work: addEventListener must be used instead.