Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I override appendChild()?

appendChild = function(message) {
    console.log("intercepted!");
}

using the code above does not seem to work.

Anyone knows?

like image 919
NoWhereToBeSeen Avatar asked Jan 10 '13 18:01

NoWhereToBeSeen


People also ask

What is the opposite of appendChild JS?

The removeChild() method removes an element's child.

How do I appendChild JavaScript?

Syntax of JavaScript appendChild:parentNode. appendChild(childNode); The childNode is the node that we want to append to the parent node parentNode . appendChild() will return the appended child.

What does appendChild mean JavaScript?

appendChild() The appendChild() method of the Node interface adds a node to the end of the list of children of a specified parent node. If the given child is a reference to an existing node in the document, appendChild() moves it from its current position to the new position.


1 Answers

What you might want to replace is Element.prototype.appendChild but it's probably a bad idea.

This example adds the text intercepted in the inserted element :

var f = Element.prototype.appendChild;
Element.prototype.appendChild = function(){f.apply(this, arguments);arguments[0].innerHTML="!Intercepted!"; };
document.body.appendChild(document.createElement("div"));

Demonstration

like image 167
Denys Séguret Avatar answered Oct 23 '22 02:10

Denys Séguret