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

131
Vistas
javascript. How to pass the "this" object after ajax call?

I have this code:

$(function(){
    $("#CASA").click(function(){
        rsExecute("rs/rs_luci.php","premi",1,function(cc){});
        var col=$( this ).css( "background-color" );
        setColor(this, 'yellow');
        setTimeout(setColor,1000,this,col);
    });
});

function setColor(obj,clr){
    $(obj).css( "background-color", clr );
}

rsExecute makes a call via ajax. function(cc){} is executed on success.

I've tried to change the code like this:

$(function(){
    $("#CASA").click(function(){
        rsExecute("rs/rs_luci.php","premi",1,function(cc){
            var col=$( this ).css( "background-color" );
            setColor(this, 'yellow');
            setTimeout(setColor,1000,this,col);
        });
    });
});

function setColor(obj,clr){
    $(obj).css( "background-color", clr );
}

but it doesn't work because this is undefined.

Is there a way to pass this object to the function inside rsExecute?

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

0

Save it to a variable, traditionally named 'that', and pass that variable using the closure.

$(function(){
    $("#CASA").click(function(){
        var that = this;
        rsExecute("rs/rs_luci.php","premi",1,function(cc){
            var col=$( that ).css( "background-color" );
            setColor(that, 'yellow');
            setTimeout(setColor,1000,that,col);
        });
    });
});
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