Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5: How to count the length of the files from the multiple-input field

How can I use jquery to count the length of the files from the multiple-input field?

alert($('#myForm input[type=file]').files.length);
<input type="file" multiple="multiple" name="file[]"/>

error:

$("#myForm input[type=file]").files is undefined

It works find with plain js but I don't want to use ID in my input field.

alert(document.getElementById('file').files.length);

<input type="file" multiple="multiple" name="file[]" id="file"/>
like image 249
Run Avatar asked Aug 21 '11 15:08

Run


People also ask

How do you limit the maximum file selected when using multiple inputs?

For limiting maximum items on multiple inputs, use JavaScript. Through this, limit the number of files to be uploaded. For example, let's say only 2 files to be uploaded at once. You can try to run the following code to learn how to use multiple attributes in HTML.

How do you select multiple inputs in HTML?

For windows: Hold down the control (ctrl) button to select multiple options. For Mac: Hold down the command button to select multiple options.

How do you input multiple files?

Tip: For <input type="file"> : To select multiple files, hold down the CTRL or SHIFT key while selecting. Tip: For <input type="email"> : Separate each email with a comma, like: [email protected], [email protected], [email protected] in the email field.

What is the input tag in HTML?

The <input> tag specifies an input field where the user can enter data. The <input> element is the most important form element. The <input> element can be displayed in several ways, depending on the type attribute.


1 Answers

Try getting the native DOM element using the .get method:

$('#myForm input[type=file]').get(0).files.length

Note however that if you have multiple DOM elements matching your selector this will return the first one and if you have none it will obviously throw an exception.

like image 50
Darin Dimitrov Avatar answered Sep 24 '22 19:09

Darin Dimitrov