Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.
The DOM offers various methods to edit the nodes on the page and alter the document tree.
With
document.createElement(): creates a new Element Nodedocument.createTextNode(): creates a new Text Node
you can create new elements and add them as children of the DOM elements you want, using document.appendChild():
const div = document.createElement('div')
div.appendChild(document.createTextNode('Hello world!'))
first.removeChild(second)removes the child node “second” from the node “first”.document.insertBefore(newNode, existingNode)inserts “newNode” as a sibling of “existingNode”, placing it before that in the DOM tree structure.element.appendChild(newChild)alters the tree under “element”, adding a new child Node “newChild” to it, after all the other children.element.prepend(newChild)alters the tree under “element”, adding a new child Node “newChild” to it, before other child elements. You can pass one or more child Nodes, or even a string which will be interpreted as a Text node.element.replaceChild(newChild, existingChild)alters the tree under “element”, replacing “existingChild” with a new Node “newChild”.element.insertAdjacentElement(position, newElement)inserts “newElement” in the DOM, positioned relative to “element” depending on the “position” parameter value. See the possible values.element.textContent = 'something'changes the content of a Text node to “something”.