8

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.

12
  • 2
    possible duplicate of Jquery Event : Detect changes to the html/text of a div, stackoverflow.com/questions/4979738/… Commented Jun 24, 2014 at 14:17
  • not possible in pure JS? Commented Jun 24, 2014 at 14:19
  • Way easier option to do this in JQuery. stackoverflow.com/questions/15657686/… Commented Jun 24, 2014 at 14:19
  • thank you all, but the topic title is: "Javascript". Commented Jun 24, 2014 at 14:26
  • This div, is it a content editable, or just a regular div? Commented Jun 24, 2014 at 14:28

5 Answers 5

15

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

use DOMCharacterDataModified to detect innerText change event

Sign up to request clarification or add additional context in comments.

1 Comment

As per W3C, Mutation Events have been deprecated in favor of MutationObserver
7

Posted on behalf of the OP.

I found a solution:

// THIRD FUNCTION (changes the text)
var i = 0;
function thirdFUN(){
    i++;
    var popo = document.getElementById('myDiv');
    popo.innerText = i;
}
setInterval(thirdFUN, 500);

//---------------------------------------------------------------

// JQUERY
/*var popo = $('#myDiv');
$('#myDiv').bind("DOMSubtreeModified",function(){
    if (popo.html() == "10")
    console.log('changed');
});
*/

//---------------------------------------------------------------

// PURE JS
window.onload = function(){

var popo = document.querySelector('#myDiv');

var observer = new MutationObserver(function(mutations){
    mutations.forEach(function(mutation){
        console.log(mutation.type); // <- It always detects changes
        if (popo.innerHTML == "10"){
            alert('hello');
        }
    });    
});

var config = {characterData: true, subtree: true};
observer.observe(popo, config);
//observer.disconnect();

}

This is also a JS Fiddle.

3 Comments

doesn't seem to work any more
@AkashJain: you may need to post a new question. If you do so, show what code you have presently and where you think it is not working specifically.
actually it the JS fiddle that is provided in the link, its not working anymore
0

The change event isn't specified to fire at any point the text changes. It fires only when the textarea loses focus (the user clicks away or tabs away). So your event will only fire when this happens.

Aside from continually checking the value of the textarea with an interval, there is no elegant way to do this (to my knowledge).

Note in this fiddle that the change event only fires after the textarea has lost focus.

See this answer for more info...

Comments

0

Can't comment on the top answer since not enough reputation, but according to MDN, Mutation Events (so including DOMCharacterDataModified) were deprecated and shouldn't be used anymore. Luckily, you can get the same thing with Mutation Observers now.

1 Comment

Could you please share example code which utilizes Mutation Observers?
-3

try

document.addEventListener("change", function() ......

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.