Vanilla JS has an awesome way to detect any DOM changes (including if a node adds any child nodes), check out: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
But these docs aren't super clear so here's a quick explanation.
First, you want to have a target node to listen to:
const targetNode = document.getElementById('some-id');
Then you want to create a trigger when the target changes (called MutationObserver):
const observer = new MutationObserver(callbackFunction);
Define what you want to happen when the target changes:
const callbackFunction = function(mutationsList) { // Use traditional 'for loops' for IE 11 for(const mutation of mutationsList) { if (mutation.type === 'childList') { console.log('A child node has been added or removed.'); } else if (mutation.type === 'attributes') { console.log('The ' + mutation.attributeName + ' attribute was modified.'); } } };
note: mutationsList is an object with loads of information what the targetNode has changed including style, addedNodes, etc...
Finally, you can start observing the target node for mutations (changes) with:
observer.observe(targetNode, { childlist: true, attributes: true });
The second parameter is an 'options' object to define what type of mutation (changes) you're looking for.
Hopefully that helps, I've found it to be a really powerful and underrated tool in js