Posts Tagged ‘AddChild’

How to add and remove elements with JavaScript?

Elements can be added and removed from the DOM using appendChild() and removeChild() methods. For example, suppose we have the following div: <div id="container"> </div> Then we can add and remove elements using JavaScript: var foo = document.createTextNode("Foo"); var hello = document.createTextNode("Hello"); var bar = document.createTextNode("Bar");   var p = document.createElement("p"); p.appendChild(foo); p.appendChild(hello); p.appendChild(bar);   [...]