Dryadwoods
Dryadwoods

Reputation: 2929

Javascript/Jquery How to catch the use of a CSS class

I am using a Javascript plugin (several lines of code) that from times to times is released a new version.

For this reason I am trying to avoid changing the original source code in order to affect my wishes.

One way that is "half" working for me is to find all the elements that are using a specific CSS class (or group of classes) and them I am removing it (or do something else with them) in order to do what I want.

The part that is not working is the "trigger/event" to process this action. During the execution of this plugin new elements are created and removed and once again I am having "wrong" entries once again.

My question: How can I "catch" all the elements that are "from a moment to the other" using the CSS class XXX? and then execute my own code.

Notes: I was reading the Jquery .on() but I need to specify an event, however the issue is that I do not know the many "places/events" from the original source code are processing this.

Update: At this point I am "manually" calling my function:

function MyOverrideAction(){
    $.each( $( ".sch-gantt-terminal" ), function( key, value ) {
      // here I have all my logic.... based on several rules (non relevant to my stackoverflow question)
    });
}

I just want that this function is executed every instance when some HTML element is using my target css class.

Upvotes: 1

Views: 145

Answers (2)

Marcos Pérez Gude
Marcos Pérez Gude

Reputation: 22158

Maybe you search something like this:

https://stackoverflow.com/a/3219767/5035890

If you listen a change in the DOM you can apply all actions that you need. With MutationObserver you can achieve it. Please, consider of the compatibility.

Good luck

Upvotes: 0

JotaBe
JotaBe

Reputation: 39004

It is much easier to redefine the CSS class after the original definition. One way to do it is to attach an inline style tag at the bottom of the document which redefines the style. You can use jQuery.append for this. For example see this.

Upvotes: 1

Related Questions