I'm trying to dynamically add some CSS classes to some elements that I don't have control over the creation of. I know that the divs I'm looking for will be added like this: mydiv.appendChild(resdiv)
. However, I cannot add code at that point at which they're created. I know that every instance of this div that is dynamically added will have a specific CSS class (let's call it output-class
). I could theoretically add CSS rules to that specific class, but I'd rather not - it would result in the duplication of a lot of styles.
What I want to do is fire an event as soon as a div with the output-class
class is added to the page. I'm trying to add some more CSS classes on the fly. Here's what I have so far:
jQuery('body').on('DOMNodeInserted', ".output-class", function(event) {
var el = $(this);
el.addClass('my new class');
// do some more manipulation of the new div (el).
});
However, as far as I can tell, this callback is never called. What am I doing wrong?