I'm trying to check multiple file upload values for an image type. With this html:
<input type="file" name="image" id="fileUpload">
And this js:
document.getElementById('fileUpload').onchange = function () {
var filename = this.value;
var a = filename.split(".");
if( a.length === 1 || ( a[0] === "" && a.length === 2 ) ) {
return "";
}
var suffix = a.pop().toLowerCase();
//if( suffix != 'jpg' && suffix != 'jpeg' && suffix != 'png' && suffix != 'gif' && suffix != 'tiff'){
if (!(suffix in {jpg:'', jpeg:'', png:'', gif:'', tiff:''})){
document.getElementById('fileUpload').value = "";
alert('Please select an image.');
}
};
Above works fine. But with multiple inputs:
<input type="file" name="image" id="fileUpload">
<input type="file" name="image" id="fileUpload">
<input type="file" name="image" id="fileUpload">
It will only work on the first. I tried changing to classes and using getElementsByClassName
, but that didn't do it and neither did wrapping it in a function and using
<input type="file" name="image" id="fileUpload" onchange="checkForImageType()">