Skip to content

Latest commit

 

History

History
35 lines (25 loc) · 949 Bytes

what-is-mutationobserver.md

File metadata and controls

35 lines (25 loc) · 949 Bytes

What is MutationObserver API

MutationObserver provides developers a way to react to changes in a DOM. It is designed as a replacement for Mutation Events defined in the DOM3 Events specification.

Basic Usage

let observerOptions = {
  childList: true,
  attributes: true,
  characterData: false,
  subtree: false,
  attributeFilter: ['one', 'two'],
  attributeOldValue: false,
  characterDataOldValue: false
};

const targetNode = document.getElementById('myId');
let observer = new MutationObserver(callback);
    
function callback (mutations) {
  // do something here
}

observer.observe(targetNode, observerOptions);

Online Demo

Reference