Adding some content to a div, whether through jQuery or via de DOM-API directly, defaults to the .appendChild()
function. What you can do is to override the .appendChild()
function of the current object and implement an observer in it. Now having overridden our .appendChild()
function, we need to borrow that function from an other object to be able to append the content. Therefor we call the .appendChild()
of an other div to finally append the content. Ofcourse, this counts also for the .removeChild()
.
var obj = document.getElementById("mydiv");
obj.appendChild = function(node) {
alert("changed!");
// call the .appendChild() function of some other div
// and pass the current (this) to let the function affect it.
document.createElement("div").appendChild.call(this, node);
}
};
Here you can find a naïf example. You can extend it by yourself I guess. http://jsfiddle.net/RKLmA/31/
By the way: this shows JavaScript complies the OpenClosed priciple. :)