javascriptappendprepend

How can I implement prepend and append with regular JavaScript?


How can I implement prepend and append with regular JavaScript without using jQuery?


Solution

  • Perhaps you're asking about the DOM methods appendChild and insertBefore.

    parentNode.insertBefore(newChild, refChild)
    

    Inserts the node newChild as a child of parentNode before the existing child node refChild. (Returns newChild.)

    If refChild is null, newChild is added at the end of the list of children. Equivalently, and more readably, use parentNode.appendChild(newChild).