A
A
ANVARD2013-12-23 16:58:20
JavaScript
ANVARD, 2013-12-23 16:58:20

How to check the number of elements in files[] array using jQuery?

Hello,

<input id="files_0" type="file" name="files[]"></input>
<input id="files_1" type="file" name="files[]"></input>

etc.

How to check the number of elements in files[] array using jQuery?

Thank you in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Melnikov, 2013-12-23
@ANVARD

check how many non-empty input elements there are in the DOM with that name

var count = $("input[name='files[]']").filter(function(el){ return $(this).val() }).length;

D
demimurych, 2013-12-23
@demimurych

As far as I know, you can get a list of files with one command only if you use this construction to upload files
but this attribute is supported by far on all browsers .
In your case, you probably have to do a direct enumeration something like this

$('input:file').each(function() { 
console.log($(this).val())
 } )
;
restrict the selector accordingly to your form, and collect the data in an array instead of console.log

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question