Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
Why is the list of selected files always empty if I obtain the file list before onchange?

I want to show names of files after I choose them. So, why is the files list empty in the onchange handler?

Here is my code:

window.onload = function () {
    let list = document.getElementById("demo");
    let files = document.getElementById("addFileId").files;

    document.getElementById("addFileId").onchange = function () {
        console.log(files);
        console.log(files[0]);

        for (let i = 0; i < files.length; i++) {
            let li = document.createElement('li');
            li.innerText = files[i].name;
            list.appendChild(li);
        }
    };
};
<input type="file" multiple class="form-control"
    name="file" id="addFileId" required>

<ul id="demo"></ul>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem is that the file list object is acquired before the change event fires. By the time the event actually happens, information in the list has become stale, while the browser already has attached a new file list to the DOM node. The solution is to obtain the file list inside the event handler:

window.onload = function () {
    let list = document.getElementById("demo");
    let addFileId = document.getElementById("addFileId");
    
    addFileId.onchange = function () {
        let files = addFileId.files;
        console.log(files);
        console.log(files[0]);

        for (let i = 0; i < files.length; i++) {
            let li = document.createElement('li');
            li.innerText = files[i].name;
            list.appendChild(li);
        }
    };
};

I’ll note, however, that amusingly, the asker’s sample works just fine, unmodified, in Firefox 91 ESR, and commenters describe it acts the same in newer versions as well. Apparently Firefox, instead of creating a new list, updates the existing file list object. Chromium behaves like described in the question; I have not tested a WebKit-based browser, but I imagine it acts like the latter. As of 2022-03-16, the HTML specification says that upon changing the file selection the browser should

Update element's selected files so that it represents the user's selection.

which can be interpreted either way. So it is probably best not to rely on either behaviour.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!