JSFiddle - React, Tailwind, and code Playground

by brigand

HTML

<textarea></textarea>
<pre id="out"></pre>

JavaScript

const targetNode = document.querySelector('textarea');

// Options for the observer (which mutations to observe)
const config = {
  attributes: true,
  childList: true,
  subtree: true
};

// Callback function to execute when mutations are observed
const callback = function(mutationsList, observer) {
  // Use traditional 'for loops' for IE 11
  for (let mutation of mutationsList) {
      out.textContent += 'mutation: ' + targetNode.value + '\n';  
    if (mutation.type === 'attributes') {
      console.log('The ' + mutation.attributeName + ' attribute was modified.');
      out.textContent += 'mutation: ' + targetNode.value + '\n';
    }
  }
};

// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);

// Start observing the target node for configured mutations
observer.observe(targetNode, config);

targetNode.addEventListener('input', () => {
  out.textContent += 'input: ' + targetNode.value + '\n';
})

setInterval(() => {
  if (targetNode.value !== 'foo')
    targetNode.value = 'foo'
}, 1000)