JavaScript Function: dupeNode()

In the process of working on my Pull-quotes plugin for WordPress, I found myself in a position where in order to move further in certain directions, I had to modify the way a standard JavaScript function works — specifically the cloneNode method of the node object.

Ultimately I decided the best way to do this would be to duplicate it as a custom function, that I could them modify to my heart’s content. This proved to be more difficult than I first thought it would be, largely due to the difficulty of debugging recursive functions (that is, functions that can refer back to themselves).

I figured I would share my handiwork with others.

Please note that as presented here, this exactly duplicates cloneNode. The only point of using this is to use it as a launching point for modifications, in order to change the way cloneNode works. (For example, you can easily insert an else if clause after the if clause if you want to monkey with the contents of text nodes.)

You can copy/paste from below, or download it here.

dupeNode function by Stephen Rider

This function exactly imitates the behavior of the cloneNode method of the Node object.  Its usefulness is in the ability for you to modify how it works.

This function may be freely modified for you own use, but please do not redistribute the unmodified script -- link to the page on instead.  If you distribute a modified version, please have the courtesy of crediting the original.

If you find (or fix!) a bug, please let me know.  Thanks!

function dupeNode($the_node,$include_all) {
// $include_all is the same as it is in cloneNode()
	var i;
	var $new_node = $the_node.cloneNode(false);
	if ($include_all == true && $the_node.hasChildNodes() == true) {
		for(i=0;i<$the_node.childNodes.length;i++) {
			$child_node = arguments.callee($the_node.childNodes[i],true);
	return $new_node;


Leave a Reply

Your email address will not be published.