I am trying to observe when the {{ cart.invoiceNumber}}
textContent changes.
using the options childList: true, subtree: true
it will detect some changes but not all of them.
and then I have tried the options characterData: false, attributes: false, childList: true, subtree: false
and this picks up the <span id="cartTotal" dangerouslySetInnerHTML={{ __html: total }} />
but it doesn't recognise when the textContent changes.
How can I use this to observe when the textContent changes?
export function elementReady(selector) {
return new Promise((resolve, reject) => {
let el = document.querySelector(selector);
if (el) {resolve(el);}
new MutationObserver((mutationRecords, observer) => {
// Query for elements matching the specified selector
Array.from(document.querySelectorAll(selector)).forEach((element) => {
resolve(element);
//Once we have resolved we don't need the observer anymore.
// observer.disconnect();
});
})
.observe(document.documentElement, {
characterData: false, attributes: false, childList: true, subtree: false
});
});
}
elementReady('#cartTotal').then((someWidget)=>{ console.log(someWidget)});
layout.js
<span dangerouslySetInnerHTML={{ __html: cart.invoiceNumber }} />