2

I have an input file upload field for single image file:

<input type="file" id="imageupload" />

To access the name of selected image file, we use

var fname = $("#imageupload")[0].files[0].name;

I don't know why [0] is used after $("#imageupload")

Similarly for multiple files upload field

<input type="file" id="imageupload" multiple />

We use (in loop)

var file_name = $("#imageupload")[0].files[i].name;

Can anyone clear my concept regarding following:

  • $("#imageupload")[0]
  • files[0]
  • files[i]
1
  • $("#imageupload") is asking jquery to select all elements that have the id of imageupload. It is possible zero or more elements will be found with that id. Thus jquery returns an array. [0] means the first element in the found array. Then you ask for the first file files[0] in that input element. If a loop, meaning multiple files, you use a variable and ask for file at the variable location files[i] where i is the variable. Whenever you see square brackets in JavaScript, and most other languages but not all, it means you are accessing an element from an array. Commented Oct 4, 2018 at 4:40

1 Answer 1

2

$("#imageupload")[0]

Basically, we don't need to use the brackets because when using the ID Selector, in the ideal world, it's supposed to be only one element match the given ID. We used it here just to make sure there's no chance for error occur. You could remove that [0] in case there's exactly one element in your website has that ID. Like this:

var fname = $("#imageupload").prop('files')[0].name;
// or
var fname = document.getElementById('fileItem').files[0].name //without jQuery

This is the demo: https://codesandbox.io/s/kx5q2x24xv

files[0] and files[i]

The files here is called a FileList object, and:

An object of this type is returned by the files property of the HTML element; this lets you access the list of files selected with the <input type="file"> element.

In other words:

All element nodes have a files array on them which allows access to the items in this list.

We use the bracket with the index in order to access the specific element of that array.

This example iterates over all the files selected by the user using an input element:

// fileInput is an HTML input element: <input type="file" id="myfileinput" multiple>
var fileInput = document.getElementById("myfileinput");

// files is a FileList object (similar to NodeList)
var files = fileInput.files;
var file;

// loop through files
for (var i = 0; i < files.length; i++) {

    // get item
    file = files.item(i);
    //or
    file = files[i];

    alert(file.name);
}

For more information: FileList

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

4 Comments

@nguyen we must need [0] in $("#imageupload")[0] otherwise I'm getting TypeError: $(...).files is undefined
I'm sorry, I've updated my answer. We have to access the FileList in the jQuery way.
But you have done it with another way $("#imageupload").prop('files')[0].name;. I still couldn't get my exact answer - why do we use [0] after id selector.
if that is the case of an array then as jquery returns files array then why [1] does not work if I have some another file upload field with same id?

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.