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

122
Visualizações
Use the element that calling eventlister inside function

How use the element that calling eventlister in a function, to append new element.

Example:

window.onload = function()
{   
    Array.from(document.getElementsByClassName('image_add_product')).forEach(function(element,index)
    {
        element.addEventListener("change", input_change, false);
    });
};

Now i need to use that element inside input_change function, to append a new element span. Using element.parentNode.insertBefore(span, this.nextSibling), get error element is not defined, using this.parentNode.insertBefore(span, this.nextSibling), get error Cannot read properties of undefined (reading 'insertBefore').

What is the correct way to do that? Pls, see the comments on code.

function input_change(evt)
{   
    //with this operator i can access the properties of element, but...       
    var file = this.files[0];
    
    if(file.size > 1572864)
    {
        console.log("Imagem exede o tamanho máximo permitido de 1,5 MB");
        this.value = "";
        return false;
    }
    var reader = new FileReader();

    reader.onload = (function(theFile)
    {   
        return function(e)
        {   
            var span = document.createElement('span');
            span.innerHTML ="<img src='...'/>";

            /*here is the problem
            how append span using element as reference*/

            this.parentNode.insertBefore(span, this.nextSibling);
        };
    })(file);
    reader.readAsDataURL(file);
    return true;
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem is this inside the onload of the file is the file, it is NOT the this outside of it.

document.querySelectorAll('.image_add_product').forEach(function(element) {
  element.addEventListener("change", input_change, false);
});

function input_change(evt) {
  var elem = this;
  var file = elem.files[0];

  if (file.size > 1572864) {
    console.log("Imagem exede o tamanho máximo permitido de 1,5 MB");
    this.value = "";
    return false;
  }
  var reader = new FileReader();

  reader.onload = function(e) {
    var span = document.createElement('span');
    span.innerHTML = "HELLO<img src='...'/>";
    elem.parentNode.insertBefore(span, elem.nextSibling);
  };
  reader.readAsDataURL(file);
  return true;
}
<label for="img1">
  <input type="file" id="img1" class="image_add_product" >
</label>

<label for="img2">
  <input type="file" id="img2" class="image_add_product" >
</label>

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