Skip to content

Instantly share code, notes, and snippets.

@helloGoGo
Last active March 30, 2017 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helloGoGo/ace65c199df0cfaf46eb7ffea6f0ce2c to your computer and use it in GitHub Desktop.
Save helloGoGo/ace65c199df0cfaf46eb7ffea6f0ce2c to your computer and use it in GitHub Desktop.
MutationObserver
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<title>MutationObserver</title>
<script src="dom_observer.js"></script>
</head>
<body>
<input type="button" value="Hello" onclick="appendTextNode()"/>
<div id="text-container"></div>
</body>
</html>
var target;
window.onload = function() {
target = document.getElementById('text-container');
var observer = new MutationObserver(function() {
alert('Mutation!');
});
observer.observe(target, { childList: true });
};
var appendTextNode = function() {
let textNode = document.createTextNode('World');
target.appendChild(textNode);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment