Last active
January 6, 2022 07:57
-
-
Save dineshdeveloper1/aa085082dae5740bff1d94ee6d7f0b24 to your computer and use it in GitHub Desktop.
Mutation Observer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <ul class="frontend"> | |
// <li>HTML5</li> | |
// <li>CSS3</li> | |
// <li>Bootstrap</li> | |
// <li>JavaScript</li> | |
// <li>jQuery</li> | |
// <li>react.js</li> | |
// </ul> | |
let myList = document.querySelector('.frontend') | |
const observer = new MutationObserver(mutations => { | |
mutations.forEach(record => { | |
// console.log(record) | |
if(record.type === 'attributes'){ | |
console.log('changes on attributes', record.attributeName) | |
} | |
if(record.addedNodes.length > 0){ | |
console.log(`Nodes Added : ${record.addedNodes.length}`) | |
} | |
if(record.removedNodes.length > 0){ | |
console.log(`Nodes removed : ${record.removedNodes.length}`) | |
} | |
}) | |
}); | |
observer.observe(myList, { | |
attributes: true, | |
childList: true | |
}) | |
setTimeout(() => { | |
myList.setAttribute('id', 'dev') | |
myList.removeChild(myList.firstElementChild) | |
const newLi = document.createElement('li') | |
newLi.textContent = 'Vue.js' | |
myList.appendChild(newLi) | |
myList.firstElementChild.textContent = "HTML6" | |
}, 2000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment