0

I have some external JS code which I don't want to modify. In multiple functions, this JS has the code to add and remove a particular class from a particular jquery element. For example:

$mck_no_messages.removeClass('vis').addClass('n-vis');
$mck_no_messages.removeClass('n-vis').addClass('vis');

I want to do some action once 'vis' class is added or removed from $mck_no_messages. One way is to implement that logic at all places where the external code is removing/adding the above class but I don't want to do that as in future they might add this condition at some more places.

how can I add some event/handler to this?

Sahil Sharma
  • 3,847
  • 6
  • 48
  • 98
  • 1
    there is some attribute observer which you can access. https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver – JavaScript Jun 09 '18 at 07:01
  • Possible duplicate of [Listen for change of class in jquery](https://stackoverflow.com/questions/3731807/listen-for-change-of-class-in-jquery) – Derlin Jun 09 '18 at 07:02
  • Possible duplicate of https://stackoverflow.com/questions/14567990/how-to-add-a-callback-function-to-the-addclass-method-of-jquery – AA Shakil Jun 09 '18 at 07:02
  • Possible duplicate of [How to add a callback function to the addClass method of jQuery?](https://stackoverflow.com/questions/14567990/how-to-add-a-callback-function-to-the-addclass-method-of-jquery) – AA Shakil Jun 09 '18 at 07:03
  • The need to do this is a "code smell" or a "design smell". Although technically speaking, yes, you can do this kind of thing with Mutation Observers, that should be necessary in only a small number of special cases. You should re-design your code, perhaps by using a function which adds or removes the class along with whatever other side-effects you want to implement. Which are what, by the way, for example? –  Jun 09 '18 at 07:17

0 Answers0