Skip to content Skip to sidebar Skip to footer

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);

Solution 1:

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.

Solution 2:

None of those answers work with the new html 5 multiple tag. So if you have something like

<inputtype="file" name="filesToUpload[]"id="filesToUpload" multiple="multiple" size="20" />

note the multiple tag, JQ fails at getting every one of this files, only gets one of them, I've pretty much tried all of the possible selectors now but to no avail.

However as mentioned before, standard Javascript works:

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

Solution 3:

you can try alert($('#myForm input[type=file]').length); or alert($('#myForm input[type=file]').size());

or alert($('#file').size()); or alert($('#file').length);

Post a Comment for "Html5: How To Count The Length Of The Files From The Multiple-input Field"