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

201
Vistas
Detect class removal by code I have no control over

I need to call a function when a class has been removed from an element, I have no control over when the class is removed.

Currently I can make it work by using setTimeout, so it's something like:

function checkClassRemoval() {
    if (!$('.myElement').hasClass('mySecondClass')) {
        // run function
    }   
}   

setTimeout(checkClassRemoval, 1000);

Is there a better way to catch the removal of the class than running a check every second?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can use MutationObserver class for this. It allow to subscribe to particular DOM element changes and fire events when this change happens:

var observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutationRecord) {
        console.log('style changed!');
    });    
});

var target = document.getElementById('someId');
observer.observe(target, { attributes : true, attributeFilter : ['style', 'className'] });

For support of this object please refer to canIuse.com

about 4 years ago · Santiago Trujillo Denunciar

0

You can override addClass/removeClass jQuery function and extend them like this:

(function(){
    var nativeMethod= jQuery.fn.addClass;

    jQuery.fn.addClass = function(){
        var result = nativeMethod.apply( this, arguments );
        jQuery(this).trigger('cssClassChanged');
        return result;
    }
})();

//Your code:
$(function(){
    $("#yourElement").bind('cssClassChanged', function(){ 
        console.log("triggered")
    });
});
about 4 years ago · Santiago Trujillo 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