By aziz punjani

2011-10-07 20:48:14 8 Comments

Are there any DOM mutation events in JQuery or in vanilla Javascript that fire cross browser?

To clarify, say I have a script on my page which inserts a div into the body. I don't have access to the script and I don't know when the div has been inserted. I was wondering if there's a DOM mutation event that I can add a listener for, to know when an element has been inserted. I know I can use a timer to periodically check for the insertion but, I don't really like the overhead that this would impose.


@Simon Steinberger 2015-04-30 07:08:06

The livequery plugin can be used for this. In version 1.x a timer was used to periodically check for any changes. However, the newer version 2.x branch seems to be working without periodical timeout (untested).

@Val 2012-09-20 09:02:46

If you are looking for an alternative to this method, here is which can also allow you to add events yourself and keep a watch for almost any change, class name change, height change, width change.

@user1028943 2012-02-22 16:43:08

JCADE (JQuery Create and Destroy Events) will do this. It uses behaviors for IE and DOM mutation events for other browsers. It does not use timing events and does not wrap JQuery methods like livequery.

    $( document ).create( "a", function( event ) {
      alert( );

@Daniel Mendel 2011-10-13 04:58:49

This is certainly a hack, but why not patch the underlying DOM methods used to insert the nodes? There are a couple ways to do this:

A. You know what specific element will be appended to:

var c = document.getElementById('#container');
c.__appendChild = c.appendChild;
c.appendChild = function(){
     alert('new item added');
     c.__appendChild.apply(c, arguments); 

fiddle demo for A

B. You know what type of element will be appended to:

HTMLDivElement.prototype.__appendChild = HTMLDivElement.prototype.appendChild;
HTMLDivElement.prototype.appendChild = function(){
    alert('new item added');

fiddle demo for B

(Note that solution B is not supported by IE < 8 or any other browser which does not support DOM prototypes.)

This same technique could just as easily be used on all the underlying DOM mutation functions such as insertBefore, replaceChild or removeChild.

That's the general idea, these demos could be adapted for pretty much any other use case -- say you want to cast a wide net and catch all additions regardless of type AND make sure it works across all browsers everything but IE < 8? (see example C below)


C. Recursively walk the DOM, swap out the function on every element to trigger a callback, and then apply the same patch to any children being appended.

var DOMwatcher = function(root, callback){
  var __appendChild = document.body.appendChild;

  var patch = function(node){
    if(typeof node.appendChild !== 'undefined' && node.nodeName !== '#text'){
      node.appendChild = function(incomingNode){
        callback(node, incomingNode);
        walk(incomingNode);, incomingNode);

  var walk = function(node){
    var i = node.childNodes.length;



DOMwatcher(document.body, function(targetElement, newElement){ 
   alert('append detected');    

$('#container ul li').first().append('<div><p>hi</p></div>');
$('#container ul li div p').append('<a href="#foo">bar</a>');

fiddle demo for C


As Tim Down commented, the above solution also won't work in IE < 8 because appendChild is not a Function and does not support call or apply. I suppose you could always fall back to the clunky but trusty setInterval method if typeof document.body.appendChild !== 'function'.

@Tim Down 2011-10-16 15:31:42

This won't work in IE < 9 and is generally dodgy since host methods such as appendChild() are not required to be Function objects (and indeed aren't in IE < 9).

@Daniel Mendel 2011-10-17 04:19:00

@TimDown Shoot! You're right, in browsers where appendChild is not a Function this solution will fail because it doesn't respond to call or apply. Good catch, I've updated the answer to reflect this. However, you'll find that the code above does work in IE 8.

@Tim Down 2011-10-17 08:30:22

Ah, OK. I was unsure about IE 8, I confess.

@Val 2012-09-20 09:02:30

If you are looking for an alternative to this method, here is which can also allow you to add events yourself and keep a watch for almost any change, class name change, height change, width change.

@Fresheyeball 2013-09-15 18:03:44

Fascinating, and terrifying. +1!

@Kai 2011-10-11 20:34:35

There are some deprecated DOM mutation events, such as DOMNodeInserted and DOMNodeInsertedIntoDocument that still work for me in Chrome 14 and IE9.

See an example at

See them all on the W3C draft at

@Tim Down 2011-10-11 23:19:51

IE 9 is the first version of IE that had DOM mutation events, so this won't work in IE < 9. While these events are deprecated, the spec for their replacement is not yet complete so they won't be going away for a while yet.

@Kai 2011-10-12 20:45:52

Great to know about IE, since I don't use it much. Btw, OT: I discovered your Rangy lib yesterday -- its awesome, ty. :)

@Adam Terlson 2011-10-07 20:51:41

Not without a plugin, I believe, but I wouldn't be surprised if I'm wrong.

My research has found a a few to choose from.

Here's one:

Here's another:

@aziz punjani 2011-10-07 21:20:18

This looked promising until i looked at the script code and realized they are not using jQuery to insert the div into the DOM.

@Adam Terlson 2011-10-07 21:31:00

Hmm, shucks. I'll admit to not having attempted this, so I can't really speak to how they work. Sorry.

Related Questions

Sponsored Content

93 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 6260287 View
  • 7817 Score
  • 93 Answer
  • Tags:   javascript arrays

27 Answered Questions

[SOLVED] What does "use strict" do in JavaScript, and what is the reasoning behind it?

58 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

41 Answered Questions

[SOLVED] Setting "checked" for a checkbox with jQuery?

56 Answered Questions

[SOLVED] How do I check if an element is hidden in jQuery?

65 Answered Questions

[SOLVED] How do I check whether a checkbox is checked in jQuery?

86 Answered Questions

[SOLVED] How do JavaScript closures work?

4 Answered Questions

17 Answered Questions

[SOLVED] How to decide when to use Node.js?

15 Answered Questions

[SOLVED] "Thinking in AngularJS" if I have a jQuery background?

Sponsored Content