1

I am programming a web and i need to know when a Class was added. I am using a JavaScript plugin and the plugin add a CSS Class in then deletes it and add it in other div (gallery). I need to know in every moment in which 'div' is this class.

Here you have an example of what I want to do

$('div').click(function() {
    $(this).toggleClass('red');
});

if ($('div').hasClass("red")) {
  $("div").addClass('border');
}
.box {
    width: 250px;
    height: 100px;
    border: 1px #000 solid;
}

.red {
    background: red;
}
.border {
  border: 6px #000 solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='box'></div>

As you see in this example, I want to add the class border only when the class red was added, can not do it together with the class red because as i say in the real page I am using a JavaScript plugin.

A J
  • 3,970
  • 14
  • 38
  • 53
TamZam
  • 69
  • 7
  • 3
    *"I need to know in every moment in wich 'div' is this class."* I bet you don't, but you think you do because of something *else* you need to do. What do you really need to do? – T.J. Crowder Sep 30 '16 at 12:36
  • 2
    Why not just add border: 6px #000 solid, to your .red class css?.. What am I missing? – Keith Sep 30 '16 at 12:38
  • Hi :) thanks for the comments. This is only an example because the page is more complicated to explane hier. As explained above. Using a (slider) plugin. This plugin add a class in the active div (the slide currently displayed) i need add other class in this moment (i want to add a .load()). But javascript is executed only one time and then don´t know when other div have after the class – TamZam Sep 30 '16 at 12:56
  • @TamZam: There is no event raised by a class change. But you can get a notification of it, through a [mutation observer](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver). – T.J. Crowder Sep 30 '16 at 13:25

1 Answers1

0

Hi :) thanks four your Ideas.

As @T.J. Crowder writed Mutation Observer is a very good option for this question.

Hier a link with the Answer: How to detect class changing by DOMAttrModified

Community
  • 1
  • 1
TamZam
  • 69
  • 7