I have a MutationObserver that observe an element with id shipping_cost and fire function updateTotal() when the innerhtml of the element changed. The MutationObserver works when the innerHTML is changed from external source (such as inspect element). But when i change the innerHTML from inside javascript, MutationObserver did not get fired, where did i do wrong?
It can be seen in the code that at the end of the javascript, i tried to change innerhtml from inside javascript, but the innerhtml of element with id total did not change, which indicates that updateTotal() did not get fired.
However, the function updateTotal() get fired when i change the innerhtml using inspect element (in chrome: right click -> inspect). How do i fix this ?
const subtotal = document.getElementById("subtotal");
const shipping_cost = document.getElementById("shipping_cost");
const total = document.getElementById("total");
const price_loading_text = "Loading . . .";
shipping_cost.innerHTML = price_loading_text;
total.innerHTML = price_loading_text;
function updateTotal() {
if(shipping_cost.innerHTML != price_loading_text) {
total.innerHTML = parseFloat(subtotal.innerHTML) + parseInt(shipping_cost.innerHTML);
} else {
total.innerHTML = price_loading_text;
}
}
updateTotal();
observer = new MutationObserver(updateTotal);
observer.observe(shipping_cost, {
attributes: false,
childList: false,
subtree: true,
characterData: true});
shipping_cost.innerHTML = "100";
<p>Subtotal: <span id="subtotal" class="subtotal">9999</span></p>
<p>Shipping_cost: <span id="shipping_cost" class="shipping_cost"></span></p>
<p>Total: <span id="total" class="total"></span></p>