Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda