Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
You can do more than use the DOM to look at a document. In fact, you can also manipulate it — creating nodes, adding them to the DOM tree, editing nodes, and removing parts of a document for which you have no more use.
In the sections that follow, I show you how to add and remove various parts of a document to and from the DOM.
Creating an Element and Other Nodes
The document object has several methods used to create nodes. I’m covering the creation of element nodes now; read about other nodes in the “Creating Other Types Nodes” section later in this chapter.
To create an element, use the document.createElement() method. This method creates an instance of the element specified by the parameter. For example, to create a p element, use this: