Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

176
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda