Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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. 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading