Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does .files[0] represent in javascript/jQuery?

So when uploading an image via ajax and I come across something like this:

$("input#uploadedfile").on("change", function(){
   var file = this.files[0],
});

Assume #uploadedfile is a file type input, is this.files[0] just targeting the first file uploaded? Also is this jQuery doing the exact same thing?:

var file = $(this).get(0).files[0]
like image 638
Zorgan Avatar asked Dec 05 '22 15:12

Zorgan


1 Answers

The files property of an input element returns a FileList. Assuming this is an input element, this.files[0] returns a File object at the index 0.

$(this).get(0) returns the first element of the jQuery object (remember that every jQuery object is also an array). So $(this).get(0).files[0] is another way of accessing the value of this.files[0].

like image 64
ughitsaaron Avatar answered Dec 08 '22 05:12

ughitsaaron