I want to fire a function when the innerHTML of a div
element changes.
Been testing with element.addEventListener('DOMCharacterDataModified', myFunction());
but it does not seem to work properly. It fires once when the page is loaded but never again after? See example code below.
Anyone have a solution to my task? Any help is greatly appreciated!
The task:
- I have an empty
div
. - At random it will be fed with data from an external source (that I do not have control of).
- Everytime when the innerHTML changes, I want to fire
myFunction()
.
My current example code:
var element = document.getElementById('div');
element.addEventListener('DOMCharacterDataModified', myFunction());
function myFunction() {
console.log(element.innerHTML);
}
setTimeout(function(){
element.innerHTML = 'Hello World!';
}, 1000);
setTimeout(function(){
element.innerHTML = 'Hello Space!';
}, 2000);
The setTimeout
s visualize the data feeding from an external source.
If possible it needs to be JS only, but jQuery could be used if really needed.
Thanks in advance!
// wirrew
.change
event? – Expectationinput
type elements. Not fordiv
s that I need it for. – Bosson