Skip to content

Instantly share code, notes, and snippets.

Created November 7, 2020 22:11
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
// Select the node that will be observed for mutations
const bank = document.getElementById('bank');
// Options for the observer (which mutations to observe)
const config = { childList: true };
// Callback function to execute when mutations are observed
const callback = function(mutationsList, observer) {
// Loop through all mutations
mutationsList.forEach(mutation => {
// Loop through all added nodes
mutation.addedNodes.forEach(node => {
// If the node is a "robber"
if ( === 'robber') {
console.log('kicking robber out of bank')
// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(bank, config);
setTimeout(()=> {
// Add a "robber" to the bank
const robber = document.createElement('div'); = 'robber';
}, 1500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment