Skip to content

Instantly share code, notes, and snippets.

@dineshdeveloper1
Last active January 6, 2022 07:57
Show Gist options
  • Save dineshdeveloper1/aa085082dae5740bff1d94ee6d7f0b24 to your computer and use it in GitHub Desktop.
Save dineshdeveloper1/aa085082dae5740bff1d94ee6d7f0b24 to your computer and use it in GitHub Desktop.
Mutation Observer
// <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