Hello,
how could I fire a event if the a css class was added or changed qith jQuery? Does changing of css class fire the jQuery change() event?
Hello,
how could I fire a event if the a css class was added or changed qith jQuery? Does changing of css class fire the jQuery change() event?
change()
does not fire when a CSS class is added or removed or the definition changes. It fires in circumstances like when a select box value is selected or unselected.
I'm not sure if you mean if the CSS class definition is changed (which can be done programmatically but is tedious and not generally recommended) or if a class is added or removed to an element. There is no way to reliably capture this happening in either case.
You could of course create your own event for this but this can only be described as advisory. It won't capture code that isn't yours doing it.
Alternatively you could override/replace the addClass()
(etc) methods in jQuery but this won't capture when it's done via vanilla Javascript (although I guess you could replace those methods too).
Whenever you change a class in your script, you could use a trigger
to raise your own event.
$(this).addClass('someClass');
$(mySelector).trigger('cssClassChanged')
....
$(otherSelector).bind('cssClassChanged', data, function(){ do stuff });
but otherwise, no, there's no baked-in way to fire an event when a class changes. change()
only fires after focus leaves an input whose input has been altered.
I would suggest you override the addClass function. You can do it this way:
// Create a closure
(function(){
// Your base, I'm in it!
var originalAddClassMethod = jQuery.fn.addClass;
jQuery.fn.addClass = function(){
// Execute the original method.
originalAddClassMethod.apply( this, arguments );
// call your function
// this gets called everytime you use the addClass method
myfunction();
}
})();
// document ready function
$(function(){
// do stuff
});