I'm trying to understand whether there's a way to register an event whenever an element's class has been changed.
I afraid of using setInterval for a check cause it might cause the browser to stuck.
Thanks.
I'm trying to understand whether there's a way to register an event whenever an element's class has been changed.
I afraid of using setInterval for a check cause it might cause the browser to stuck.
Thanks.
If you are using jQuery. This could be a solution:
https://github.com/aheckmann/jquery.hook/blob/master/jquery.hook.js
I wouldn't recommend overwriting methods that you don't own but it is possible to do something like this:
//Overide addClass Method
(function(){
var addClassMethod = jQuery.fn.addClass;
jQuery.fn.addClass = function(){
var result = addClassMethod.apply( this, arguments );
jQuery(this).trigger('classChanged');
return result;
}
})();
$(".yourElemClass").bind('classChanged', function(){
//Your code goes here
});
One thing you could do is create a changeClass function :
function changeClass(element, className) {
element.className = className;
// Your code to run on the class change
}
Then, when you need to change the class of an element just :
changeClass(myElement, "myClass");