How to write an element just after another with javascript?

DiegoP. picture DiegoP. · May 30, 2011 · Viewed 60.6k times · Source

I need to append an element onclick just after another (a textarea).

How do I do that?

Answer

RobG picture RobG · May 30, 2011

The accepted answer will work in this specific case, but to insert an element after another more generally, the following does the job:

someElement.parentNode.insertBefore(newElement, someElement.nextSibling);

Where newElement is the element to be inserted and someElement is the element it is to be inserted after.

W3C insertBefore method of the Node interface