Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.insertBefore throws error

Tags:

javascript

I have this piece of code:

  <textarea id="test" style="width: 400px; height: 100px"></textarea>
  <script>
    var inserting = document.createElement("div");
    document.insertBefore(inserting,document.getElementById("test"));
  </script>

Which should insert DIV id=inserting before textarea id=test, but this error occurs

Node was not found" code: "8

I use FireFox 3.6 with Firebug on WinXP. Where is the problem?

like image 972
Jan Turoň Avatar asked Dec 07 '22 01:12

Jan Turoň


1 Answers

insertBefore needs to called on the parent element of the element before which is inserted:

<textarea id="test" style="width: 400px; height: 100px"></textarea>
  <script>
    var inserting = document.createElement("div");
    var insertAt = document.getElementById("test");
    insertAt.parentNode.insertBefore(inserting,insertAt);
  </script>
like image 104
RoToRa Avatar answered Dec 17 '22 13:12

RoToRa