As others have mentioned, you need to call insertBefore
on the parent of the <textarea>
node. According to the API documentation for Node.insertBefore:
Summary
Inserts the specified node before a reference element as a child of the current node.
Syntax
var insertedElement = parentElement.insertBefore(newElement, referenceElement);
If referenceElement is null, newElement is inserted at the end of the list of child nodes.
insertedElement
The node being inserted, that is newElement
parentElement
The parent of the newly inserted node.
newElement
The node to insert.
referenceElement
The node before which newElement is inserted.
So you want to say something along the lines of:
parent_element_goes_here.insertBefore(inserting,document.getElementById("test"));
Instead of:
document.insertBefore(inserting,document.getElementById("test"));
Also, your code may be executing before the DOM has finished being loaded. You can ensure all of the DOM elements are loaded by using the
window.onload event handler. From the MDC docs:
The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images and sub-frames have finished loading.
So instead of calling your code directly, you would do this:
window.onload=function(){
...
}