Trigger JavaScript event if div innerText change

user3746998 picture user3746998 · Jun 24, 2014 · Viewed 14.3k times · Source

I want to make an alert when a div's innerText changes:

myDiv.addEventListener("change", function() {
    if (myDiv.innerText != "some text")
        alert('innerText has changed.');
},false);

Does not work, please help.

Answer

Vishal choudhary picture Vishal choudhary · Oct 23, 2018

myDiv.addEventListener("DOMCharacterDataModified", function (event) { // ... add your code he }, false);

use DOMCharacterDataModified to detect innerText change event