Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of files in input box with jQuery?

How can I count the number of files that are in the file upload input using jQuery?

I have tried this but it always returns 1:

$("body").on("change", ".create-album .custom-file-input .createAlbumFileUpload", function(){   

    var numFiles = $("input:file", this).length;
    alert(numFiles);
});

My HTML:

<form enctype="multipart/form-data" class="createAlbumFileUpload">
    <input type="file" name="uploadFile[]" multiple="multiple"/>
</form>

I found this jsFiddle on another question asking the same thing, however I can't see why mine isn't working?

like image 240
Dylan Cross Avatar asked Jan 21 '13 22:01

Dylan Cross


1 Answers

The fiddle you provided contains the answer.

var numFiles = $("input:file", this)[0].files.length;

http://jsfiddle.net/xvLAc/1/

like image 88
Kevin B Avatar answered Sep 23 '22 04:09

Kevin B