Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an element just after another with javascript? [duplicate]

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

How do I do that?

like image 337
DiegoP. Avatar asked May 30 '11 08:05

DiegoP.


People also ask

How to use after in JavaScript?

The Element. after() method inserts a set of Node or string objects in the children list of the Element 's parent, just after the Element . String objects are inserted as equivalent Text nodes.

How do you insert an element to your first child?

To insert element as a first child using jQuery, use the prepend() method. The prepend( content ) method prepends content to the inside of every matched element.


1 Answers

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

like image 80
RobG Avatar answered Nov 07 '22 07:11

RobG