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"/>
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.
For windows: Hold down the control (ctrl) button to select multiple options. For Mac: Hold down the command button to select multiple options.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With