Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!