I have gone through so many convoluted answers to this question and no one seems to be able to give a simple solution....
I typically use a setTimeout function to wait for all elements on a page to load before doing some work. My job requires me to wait for the angular app to load before I can manipulate things in the DOM. Using a setTimeout generally works but every now and again a customer is using a potato with a screen and when their CPU is as slow as a snail my DOM manipulation functions fail to execute in time.
The following block of code works for what I need it to do in 99% of instances but in this particular case the customer is reporting that it is not working on their end:
setTimeout(() => {
const commentLabel = document.querySelectorAll('.donation-label');
commentLabel.forEach((element) => {
if (element.textContent.includes('Leave a comment')) {
element.innerHTML = 'Tell us how to improve';
}
});
}, 750);
Instead of using a setTimeout where I am waiting for some duration of time to execute the "work", I want to use something that simply waits for the element with ".donation-label" (or any other selector of my choosing) to exist and then do something.
This needs to be in vanilla JS (no Jquery). I'm finding answers related to MutationObservers and async but can't find anything simple or anything that even works for that matter. Please help. Thank you very much!