# 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. First we'll see a simple example and then explain the methods. ## Example: show a message For a start, let's see how to add a message on the page that looks nicer than `alert`. Here's how it will look: ```html autorun height="80" *!*
Hello
Bye
``` That's how we can append an arbitrary HTML to our page. Here's the picture of insertion variants:  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 ``` ## 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"Somewhere in the page...
*!* */!*The end
``` The call to `document.write(html)` writes the `html` into page "right here and now". The `html` string can be dynamically generated, so it's kind of flexible. We can use JavaScript to create a full-fledged webpage and write it. The method comes from times when there was no DOM, no standards... Really old times. It still lives, because there are scripts using it. In modern scripts we can rarely see it, because of the following important limitation: **The call to `document.write` only works while the page is loading.** If we call it afterwards, the existing document content is erased. For instance: ```html runAfter one second the contents of this page will be replaced...
*!* */!* ``` So it's kind of unusable at "after loaded" stage, unlike other DOM methods we covered above. That was the downside. Technically, when `document.write` is called while the browser is reading ("parsing") incoming HTML, and it writes something, the browser consumes it just as if it were initially there, in the HTML text. That gives us the upside -- it works blazingly fast, because there's *no DOM modification*. It writes directly into the page text, while the DOM is not yet built, and the browser puts it into DOM at generation-time. So if we need to add a lot of text into HTML dynamically, and we're at page loading phase, and the speed matters, it may help. But in practice these requirements rarely come together. And usually we can see this method in scripts just because they are old. ## Summary Methods to create new nodes: - `document.createElement(tag)` -- creates an element with the given tag, - `document.createTextNode(value)` -- creates a text node (rarely used), - `elem.cloneNode(deep)` -- clones the element, if `deep==true` then with all descendants. Insertion and removal of nodes: - From the parent: - `parent.appendChild(node)` - `parent.insertBefore(node, nextSibling)` - `parent.removeChild(node)` - `parent.replaceChild(newElem, node)` All these methods return `node`. - Given a list of nodes and strings: - `node.append(...nodes or strings)` -- insert into `node`, at the end, - `node.prepend(...nodes or strings)` -- insert into `node`, at the beginning, - `node.before(...nodes or strings)` –- insert right before `node`, - `node.after(...nodes or strings)` –- insert right after `node`, - `node.replaceWith(...nodes or strings)` –- replace `node`. - `node.remove()` –- remove the `node`. Text strings are inserted "as text". - Given a piece of HTML: `elem.insertAdjacentHTML(where, html)`, inserts depending on where: - `"beforebegin"` -- insert `html` right before `elem`, - `"afterbegin"` -- insert `html` into `elem`, at the beginning, - `"beforeend"` -- insert `html` into `elem`, at the end, - `"afterend"` -- insert `html` right after `elem`. Also there are similar methods `elem.insertAdjacentText` and `elem.insertAdjacentElement`, they insert text strings and elements, but they are rarely used. - To append HTML to the page before it has finished loading: - `document.write(html)` After the page is loaded such a call erases the document. Mostly seen in old scripts.