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

258
Visualizações
Javascript: node.onclick call a function not working

I use a loop through the node elements to get the ID from the clicked element. Until then all good and it works. Now I want to call an another function in my class & get this error:

this._PreLoad is not defined at HTMLElement.s.onclick

this._joinnews.forEach((node) => {
    node.onclick = function(){
        const requestData = `newsid=${node.id}`;
        // some code // 
        this._PreLoad(true); // ERROR
        // _PreLoad(true); not working
        // this._PreLoad(true).bind(this); not working (I am not yet familiar with the bind function)
    }
});

Are the functions inside a node loop unknown ?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The correct place to bind this is here:

node.onclick = function() {
    const requestData = `newsid=${node.id}`;
    // some code // 
    this._PreLoad(true);
}.bind(this); // bind here!

Alternatively, use an arrow function which preserves this as whatever it was at the time of the function declaration:

node.onclick = () => {
    const requestData = `newsid=${node.id}`;
    // some code // 
    this._PreLoad(true);
}
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