user2739275
user2739275

Reputation:

how to get the length of the selected files of "file input"

With javascript I can get the number of files selected by the user on a multiple input. Here is the script:

var file = document.getElementById('file'); 

    // check for the amount of the files the user can upload
    console.log(file.files.length);

And it works. But when I use it in jQuery it does not work. it does not show the files length nor anything else. However, for jQuery I tried to use the following code:

var files;

$('#file').on('change', function(event){
        files = event.target.files;
    });

$('#uploadFiles').on('click', function(e){          
            console.log(files.length);
    });

But the console message is: files is not defined;

And this is my input field:

<input type="file" class="form-file" id="file" multiple style='display: none;' name="file" />

Upvotes: 1

Views: 4055

Answers (1)

Andy
Andy

Reputation: 1043

http://jsfiddle.net/z2v66/1/ It seems to be working.

Variable files is available only in this anonymous function. Define it (var files) outside that function.

Upvotes: 2

Related Questions