I am trying to write a script that will replace one element with another any time it exists. This means I need to replace that element as soon as it is created, including when the page loads.
I have tried using MutationObserver to no avail.
var target = document.querySelector('#taskboard');
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if($(mutation.target).is('div.someClass')){
//Do a thing
}
});
});
config = { attributes: true, childList: true, characterData: true , subtree: true};
observer.observe(target, config);
I could obviously just check for the element every second and replace it, but I doubt that's terribly performant, and it is certainly not elegant.
How can I detect the creation or existence of an element at any time so that I may replace it with another element?