A partial archive of discourse.wicg.io as of Saturday February 24, 2024.

Is the MutationObserver attributeOldValue example incorrect on Mozilla MDN?

trusktr
2020-03-09

On this page, there’s this example:

function callback(mutationList) {
  mutationList.forEach(function(mutation) {
    switch(mutation.type) {
      case "attributes":
        notifyUser("Attribute name " + mutation.attributeName +
            " changed to " + mutation.target[mutation.attributeName] +
            " (was " + mutation.oldValue + ")");
        break;
    }
  });
}

var targetNode = document.querySelector("#target");

var observer = new MutationObserver(callback);
observer.observe(targetNode, {
  attributes: true,
  attributeOldValue: true
});

For me, mutation.target is an Element, not an attribute node, and the expression mutation.target[mutation.attributeName] simply returns undefined because that is not how attribute on an element are accessed.