thom thom - 1 year ago 181
HTML Question

HTML5 FileList and JQuery's each

I grab the filelist ( and then I want to use JQuery's each function.

var file_list = $(this).attr('files');
/* Code goes here */
/* Code goes here */

I get the following error:

Uncaught TypeError: Object #<FileList> has no method 'each'

Any idea? Thank you.

Answer Source

It is because $(this).attr('files'); just returns the plain DOM file list, and not a jQuery object.

Either you need to loop over it the old fashioned way:

for(var i=0,file;file=file_list[i];i++) {
 //do your thing

or you could use $.each:

     //do your thing

Be aware that $.each is slower than the plain for loop, and in this case gives you very little convenience, so i'd stick with the for loop.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download