Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how would I detect if "multiple" attribute is supported for file input elements?

Internet Explorer does not support the multiple attribute for <input type="file" />. However, its not only IE that lacks this support... also certain mobile browsers do not support the multiple attribute. So simply detecting that the browser is IE is not the ideal solution.

So how would I detect if the multiple attribute is supported for for <input type="file" /> with JavaScript?

UPDATE

It seems like Modernizr has support for new HTML5 input element attributes:

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

The accepted solution seems to work, however, since I'm already using Modernizr, my solution is the following:

/**
 * Determines if the given attribute is supported for <input /> elements.
 * 
 * @param attribute - the attribute to test for (ex. "multiple")
 */
function isInputAttributeSupported(attribute) {
    return (Modernizr.input[attribute]) ? true : false;
};
like image 456
Hristo Avatar asked May 05 '12 00:05

Hristo


People also ask

What is use of multiple attribute of input tag?

The multiple attribute is a boolean attribute. When present, it specifies that the user is allowed to enter more than one value in the <input> element. Note: The multiple attribute works with the following input types: email, and file.

Can an element have multiple attributes?

The multiple attribute in HTML allows user to enter more than one value. It is a Boolean attribute and can be used on <input> as well as <select> element, To allow multiple file uploads in HTML forms, use the multiple attribute. The multiple attribute works with email and file input types.

What attributes is used to input data into a file?

The input element, having the "file" value in its type attribute, represents a control to select a list of one or more files to be uploaded to the server. When the form is submitted, the selected files are uploaded to the server, along with their name and type.

Which input type is used for multiple selection?

HTML 5 has a property for input tag which is 'multiple'.


2 Answers

You can try checking for the existence of the corresponding property:

var supportsMultipleFiles = 'multiple' in document.createElement('input');

Example: http://jsfiddle.net/sbZvS/

like image 70
Jonathan Lonowski Avatar answered Sep 24 '22 01:09

Jonathan Lonowski


var inp = document.createElement("input");
inp.setAttribute("multiple", "true");
var supportsMultiple = inp.multiple===true;
like image 45
epascarello Avatar answered Sep 26 '22 01:09

epascarello