Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
13
rated 0 times [  20] [ 7]  / answers: 1 / hits: 144272  / 10 Years ago, thu, may 1, 2014, 12:00:00

I am having an issue with JavaScript. I'm getting this error message:



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.



Javascript:


var vidCounter = 0;
vidCounter++;

var originalDiv;
var newVideo = document.createElement("video");

newVideo.setAttribute("name", vidCounter);
newVideo.setAttribute("autoplay", "true");
originalDiv = document.getElementById("othersarea");
document.body.insertBefore(newVideo, originalDiv);

It is trying to add a <video> tag below a div called othersarea.


<div id="remoteVideos">
<div class="title">
<h2>Others</h2>
</div>
<div id="othersarea"></div>
</div>

How do I fix this?


I also want to run attachMediaStream([VIDEO TAG HERE HOW?], event.stream); on my new video tag.


More From » jquery

 Answers
20

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


[#71225] Tuesday, April 29, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rociom

Total Points: 287
Total Questions: 88
Total Answers: 101

Location: Oman
Member since Wed, Nov 17, 2021
3 Years ago
;