Is there any way to detect when a CSS class is removed from a DOM element?
I have a <ul>
, and I need to capture when this <ul class="open">
has its open
class removed.
Any way to do this in Angular or jQuery
Is there any way to detect when a CSS class is removed from a DOM element?
I have a <ul>
, and I need to capture when this <ul class="open">
has its open
class removed.
Any way to do this in Angular or jQuery
If you're talking about monitoring when the class attribute changes without any help from whomever is modifying it, then the only way to do that would be to:
Solve your problem entirely with CSS (where the CSS styles will change automatically when a class is removed)
With a MutationObserver (in modern browsers only) where you can register an interest in that particular attribute change. Unfortunately, MutationObservers are not supported in IE before IE 11.
If you are more specific about what exactly you're trying to accomplish and/or what code is changing the classname, then we could probably offer some other ideas too.
you can write custom directive for this
app.directive("isNotOpen", function(){
var linkFunction = function(scope, element, attrs){
scope.$watch(
function() {
return element.attr('class');
}, function(){
if(!element.hasClass("open")){
scope.$eval(scope.isNotOpen)
}
});
};
return {
link: linkFunction
scope: {isNotOpen: '&'}
}
})
and you can use is like below
<div class="open" is-not-open="dosomething()">
You could $watch
the class attribute with a directive like this :
.directive('watchClass', function() {
return function(scope, element, attrs) {
scope.$watch(function() {
return attrs['class']; // Or element.attr('class');
}, function(newValue, oldValue) {
// Check for open class removal and do something
});
};
});
And then apply that directive to your elements.
<ul class="open" watchClass>
(Note: having anything else than angular torturing your DOM sounds weird to me)