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

156
Vistas
How do I access an element created by javascript for upload?

I am trying to automatically prompt a user to upload a CSV file and then I plan to access the data within, but I am unable to do this. What am I doing wrong? input.name is always undefined and viewing the whole object doesn't provide any relevant details.

The source of this query primarily came from the answer to this question Open file dialog box in JavaScript. I am trying to achieve this purely in javascript, not HTML.

jsfiddle

$(document).ready(function() {
        var input = $(document.createElement('input')); 
    input.attr("type", "file");
    input.on('change',function(){
        alert(JSON.stringify(input.name, null, 4));
        alert(JSON.stringify(input, null, 4));
        });
    input.trigger('click');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

input is the return value of $(). It is a jQuery object not a DOM object. To access the name property of the underlying DOM object, use the prop method.

input.prop('name')
about 4 years ago · Juan Pablo Isaza Denunciar

0

I finally got it working. Here is the solution

$(document).ready(function() {
  var input = $(document.createElement('input'));
  input.attr("type", "file");
  input.on('change', function() {
    var csvFile = input[0].files[0];
    var ext = csvFile.name.split(".").pop().toLowerCase();

    if (ext != "csv") {
      alert('upload csv');
      return false;
    }
    if (csvFile != undefined) {
      reader = new FileReader();
      reader.onload = function(e) {
        var data = $.csv.toObjects(e.target.result);
        alert(JSON.stringify(data, null, 4));

        $.each(data, function(index, val) {
          //do something with each row of data val.ColumnName

        });
      }
      reader.readAsText(csvFile);
    }
  });
  input.trigger('click');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-csv/1.0.21/jquery.csv.js"></script>
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