6

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?

1
  • Yours doesn't look at the files of the input in the same way that the fiddle does. Commented Jan 21, 2013 at 22:51

3 Answers 3

20

The fiddle you provided contains the answer.

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

Ah that is odd. You did remove the extra selectors didn't you?
1

You can also try with this,

Take the input in Div and give an id,

var count =  $('#divPhoto').children().length; // You'll get the length in this.

Comments

0

You can easily check with below code and https://jsfiddle.net/vvz3a4qp/2/ :

HTML:

<form name="myForm" enctype="multipart/form-data">
<input type="text" name="name">
<input type="file" name="name1" >
<input type="file" name="name2" ><br><br>
<span id="total"></span>
</form>

JS:

 $("body").change(function(){
            alert($(":file").length);
            $("#total").html($("input[type=file]").length);
      });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.