0

I've added a form to my webpage which allows for multiple file uploads. I added the input <input type="file" name="photo_upload[]" id="photo_upload" multiple="multiple" />.

I've tried to add a warning which is only visible if the browser does not support the multiple file feature. I tried this code:

<script type="text/javascript">
var testrange=document.createElement("input")
testrange.setAttribute("multiple", "multiple")
if (testrange.multiple=="multiple"){
    document.getElementById('multiple_warning').style.display = 'none';
}
</script>
<div id="multiple_warning" style="background:#FFFF99; border:dashed; border-width:thin;">Sorry, but your browser does not support the multiple file upload feature.<br />
If you have more than one photo to send us, please send it after you recieved your comformation email.</div>

but it always displays the "multiple_warning" regardless. How can I get this to behave properly?

sticks
  • 57
  • 9
  • You can use the MODERNIZR Library like in this [example](http://stackoverflow.com/questions/8010122/modernizr-just-for-one-quick-check) – SpecialHias Jul 10 '12 at 06:04

3 Answers3

1

You could use Modernizr to test for particular attribute. Its an open source, cross browser html 5 detection library

if(Modernizr.input[attribute])){
    alert("Attribute exists");
}
else{
    //error handling
}

See here http://modernizr.com/docs/#input

Anand
  • 14,545
  • 8
  • 32
  • 44
0

Check the console to see why your test always failes ;)

testrange.setAttribute("multiple", "multiple")
console.log(testrange.multiple);

You could check this feature with this snippet

if (!("multiple" in testrange)) {
    alert("no multiple upload supported");
}
Andreas
  • 21,535
  • 7
  • 47
  • 56
0

Use this instead of the JavaScript you have:

var testrange = document.createElement("input")
if ("multiple" in testrange) {
    document.getElementById("multiple_warning").style.display = "none";
}

If you're doing a lot of feature detection, consider using Modernizr to test these sorts of things rather than creating your own tests. It accounts for a lot of corner cases and will save you a lot of headaches.

Trott
  • 66,479
  • 23
  • 173
  • 212