Uncaught NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node
You have to call insertBefore
on the parent element of the element you're inserting before. document.body
is not the parent, it's far up in the DOM hierarchy.
And to insert after a DIV, you have to insert before its next sibling.
var parentDiv = document.getElementById("remoteVideos");
parentDiv.insertBefore(newVideo, originalDiv.nextSibling);
See the examples in MDN
Try using the parentNode:
originalDiv.parentNode.insertBefore(newVideo, originalDiv);
This will insert newVideo directly in front of the originalDiv.
The reason is that node.insertBefore(newNode, existingNode)
uses node
as a reference to the element that surrounds the existingNode
.
Ref: http://www.w3schools.com/jsref/met_node_insertbefore.asp
It's also what Google Analytics does: http://www.stevesouders.com/blog/2010/05/11/appendchild-vs-insertbefore/