'JavaScript: on element removed event listener

What is the core javascript equivelent to the jquery's remove event listener

$(element).on('remove', someFunction);



Solution 1:[1]

Here's how to do it with a Mutation Observer:

function onElementRemoved(element, callback) {
  new MutationObserver(function(mutations) {
    if(!document.body.contains(element)) {
      callback();
      this.disconnect();
    }
  }).observe(element.parentElement, {childList: true});
}

// Example usage: 
onElementRemoved(yourElement, function() {
  console.log("yourElement was removed!");
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 joe