# Modifying the document DOM modifications is the key to create "live" pages. Here we'll see how to create new elements "on the fly" and modify the existing page content. ## Example: show a message Let's see the methods on example. We'll add a message on the page that looks nicer than `alert`. Here's how it will look: ```html autorun height="80" *!*
Hi there! You've read an important message.
*/!* ``` That was an HTML example. Now let's create the same `div` with JavaScript (assuming that the styles are in the HTML or an external CSS file). ## Creating an element To create DOM nodes, there are two methods: `document.createElement(tag)` : Creates a new *element node* with the given tag: ```js let div = document.createElement('div'); ``` `document.createTextNode(text)` : Creates a new *text node* with the given text: ```js let textNode = document.createTextNode('Here I am'); ``` ### Creating the message In our case the message is a `div` with `alert` class and the HTML in it: ```js let div = document.createElement('div'); div.className = "alert"; div.innerHTML = "Hi there! You've read an important message."; ``` We created the element, but as of now it's only in a variable. We can't see the element on the page, as it's not yet a part of the document. ## Insertion methods To make the `div` show up, we need to insert it somewhere into `document`. For instance, in `document.body`. There's a special method `append` for that: `document.body.append(div)`. Here's the full code: ```html run height="80" ``` This set of methods provides more ways to insert: - `node.append(...nodes or strings)` -- append nodes or strings at the end of `node`, - `node.prepend(...nodes or strings)` -- insert nodes or strings into the beginning of `node`, - `node.before(...nodes or strings)` –- insert nodes or strings before the `node`, - `node.after(...nodes or strings)` –- insert nodes or strings after the `node`, - `node.replaceWith(...nodes or strings)` –- replaces `node` with the given nodes or strings. Here's an example of using these methods to add items to a list and the text before/after it: ```html autorun
  1. 0
  2. 1
  3. 2
``` Here's a visual picture what methods do: ![](before-prepend-append-after.svg) So the final list will be: ```html before
  1. prepend
  2. 0
  3. 1
  4. 2
  5. append
after ``` These methods can insert multiple lists of nodes and text pieces in a single call. For instance, here a string and an element are inserted: ```html run
``` All text is inserted *as text*. So the final HTML is: ```html run *!* <p>Hello</p> */!*
``` In other words, strings are inserted in a safe way, like `elem.textContent` does it. So, these methods can only be used to insert DOM nodes or text pieces. But what if we want to insert HTML "as html", with all tags and stuff working, like `elem.innerHTML`? ## insertAdjacentHTML/Text/Element For that we can use another, pretty versatile method: `elem.insertAdjacentHTML(where, html)`. The first parameter is a code word, specifying where to insert relative to `elem`. Must be one of the following: - `"beforebegin"` -- insert `html` immediately before `elem`, - `"afterbegin"` -- insert `html` into `elem`, at the beginning, - `"beforeend"` -- insert `html` into `elem`, at the end, - `"afterend"` -- insert `html` immediately after `elem`. The second parameter is an HTML string, that is inserted "as HTML". For instance: ```html run
``` ...Would lead to: ```html run

Hello

Bye

``` That's how we can append an arbitrary HTML to the page. Here's the picture of insertion variants: ![](insert-adjacent.svg) We can easily notice similarities between this and the previous picture. The insertion points are actually the same, but this method inserts HTML. The method has two brothers: - `elem.insertAdjacentText(where, text)` -- the same syntax, but a string of `text` is inserted "as text" instead of HTML, - `elem.insertAdjacentElement(where, elem)` -- the same syntax, but inserts an element. They exist mainly to make the syntax "uniform". In practice, only `insertAdjacentHTML` is used most of the time. Because for elements and text, we have methods `append/prepend/before/after` -- they are shorter to write and can insert nodes/text pieces. So here's an alternative variant of showing a message: ```html run ``` ## Node removal To remove a node, there's a method `node.remove()`. Let's make our message disappear after a second: ```html run untrusted ``` Please note: if we want to *move* an element to another place -- there's no need to remove it from the old one. **All insertion methods automatically remove the node from the old place.** For instance, let's swap elements: ```html run height=50
First
Second
``` ## Cloning nodes: cloneNode How to insert one more similar message? We could make a function and put the code there. But the alternative way would be to *clone* the existing `div` and modify the text inside it (if needed). Sometimes when we have a big element, that may be faster and simpler. - The call `elem.cloneNode(true)` creates a "deep" clone of the element -- with all attributes and subelements. If we call `elem.cloneNode(false)`, then the clone is made without child elements. An example of copying the message: ```html run height="120"
Hi there! You've read an important message.
``` ## DocumentFragment [#document-fragment] `DocumentFragment` is a special DOM node that serves as a wrapper to pass around lists of nodes. We can append other nodes to it, but when we insert it somewhere, then its content is inserted instead. For example, `getListContent` below generates a fragment with `
  • ` items, that are later inserted into `