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

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

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 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