Search code examples
javascriptappendchild

Append child at node x?


I know that to remove a child node at a certain position in the DOM tree I can do this;

this.element.removeChild(this.element.childNodes[index]);

I am trying to figure out if there is a way to append child at node x like above but in reverse. In pseudo code it would be something like (I think);

this.element.appendChild(child at node[index]));

Thanks for helping me with this.


Solution

  • Maybe you are looking for insertBefore:

    this.element.insertBefore(element, this.element.childNodes[index]);