The DOM starting point
A loaded page is an HTML document that the browser turns into a tree of objects — the Document Object Model — structured as parent-child relationships. Within that tree the objects are called nodes, and an HTML element is a specific kind of node: from most general to most specific, Object → Node → Element → HTML Element. In a parent div containing a span, the span is the child and the div is the parent. Adding content after load means creating or locating such objects and placing them in the tree.

<!-- I'm the parent element -->
<div>
<!-- I'm a child element -->
<span>Hello</span>
</div>
Getting a handle on elements, and what methods accept
The append and insert methods generally share a shape:
Element.append_method_choice(stuff_to_append)
To get the matched element, the DOM can be queried with CSS selectors, an ID, or a class:
<div id="example" class="group">
Hello World
</div>
// Query a specific selector (could be class, ID, element type, or attribute):
const my_element1 = document.querySelector('#example')
// Query an element by its ID:
const my_element2 = document.getElementbyId('example')
// Query an element by its class:
const my_element3 = document.getElementsbyClassName('group')[0]
All three lines above target the same thing by different routes. Note that getElementByClass returns an array, since it can match multiple elements and must account for all of them.
Parameters fall into three kinds:
- Nodes — created with
document.createElement()or returned by a query method. - Plain text — text carrying no tags or formatting, e.g.
Hello. - HTML — text that is parsed as markup when added, e.g.
<div>Hello</div>.
Not every method accepts every kind:
// Append Something
const my_element1 = document.querySelector('#example')
my_element1.append(something)
| Method | Node | HTML Text | Text |
|---|---|---|---|
append | Yes | No | Yes |
appendChild | Yes | No | No |
insertAdjacentHTML | No | Yes | Yes1 |
innerHTML2 | No | Yes | Yes |
insertAdjacentText is recommended.2 Instead of taking traditional parameters,
innerHTML is used like: element.innerHTML = 'HTML String'Choosing a method
The decision follows from what you're adding and which browsers you must support:
- HTML already handed to your JavaScript is easiest to handle with methods that accept HTML.
- Large markup built in JavaScript gets verbose when you work with nodes, while an HTML string is less so.
- If event listeners must be attached right away, work with nodes, since
addEventListeneris called on nodes, not on HTML strings. - When only text is needed, any method that takes plain text works.
- HTML from an untrusted source, such as user comments, is dangerous unless it has been sanitized first.
- For Internet Explorer support,
appendcannot be used.
The buddy list worked four ways
Take a chat application that adds a user, Dale, to a buddy list on login:
<!-- HTML Buddy List -->
<ul id="buddies">
<li><a>Alex</a></li>
<li><a>Barry</a></li>
<li><a>Clive</a></li>
<!-- Append next user here -->
</ul>
append
This requires building a node that resolves to <li><a>Dale</a></li>:
const new_buddy = document.createElement('li')
const new_link = document.createElement('a')
const buddy_name = "Dale"
new_link.append(buddy_name) // Text param
new_buddy.append(new_link) // Node param
const list = document.querySelector('#buddies')
list.append(new_buddy) // Node param
The user lands at the end of the list, before the closing </ul>. The prepend method would place it at the front instead. append can also fill the <a> tag with text directly:
const buddy_name = "Dale"
new_link.append(buddy_name) // Text param
That flexibility comes with a cost: append is unsupported in Internet Explorer.
appendChild
This method accepts node objects only, so plain text has to come through textContent or innerText. Unlike append, it is supported in Internet Explorer.
const new_buddy = document.createElement('li')
const new_link = document.createElement('a')
const buddy_name = "Dale"
new_link.textContent = buddy_name
new_buddy.appendChild(new_link) // Node param
const list = document.querySelector('#buddies')
list.appendChild(new_buddy) // Node param
With heavier markup, the node-based approach grows unwieldy. Say the target is not a single list item but this:
<li class="abc">
<a id="user_123" class="def">
<img src="images/dale.jpg" alt="Profile Picture"/>
<span>Dale</span>
</a>
</li>
Then the JavaScript becomes something like:
const buddy_name = "Dale"
const new_buddy = document.createElement('li')
new_buddy.className = 'abc'
new_buddy.setAttribute('data-tooltip', `Click for ${buddy_name}`)
const new_link = document.createElement('a')
new_link.id = 'user_123'
new_link.className = 'def'
new_link.setAttribute('data-user', buddy_name)
const new_profile_img = document.createElement('img')
new_profile_img.src = 'images/dale.jpg'
new_profile_img.alt = 'Profile Picture'
const new_buddy_span = document.createElement('span')
new_buddy_span.textContent = buddy_name
new_link.appendChild(new_profile_img) // Node param
new_link.appendChild(new_buddy_span) // Node param
new_buddy.appendChild(new_link) // Node param
const list = document.querySelector('#buddies')
list.appendChild(new_buddy) // Node param
The exact code isn't the point — building a lot of HTML from JavaScript methods is cumbersome, and neither append nor appendChild avoids it. In such cases it would be nicer to write the HTML as a string.
insertAdjacentHTML
This method also adds content to DOM elements, but at a position specified relative to the matched element, and it accepts HTML. Four positions are available:
<!-- beforebegin -->
<div id="example" class="group">
<!-- afterbegin -->
Hello World
<!-- beforeend -->
</div>
<!-- afterend -->
Inserting at the beforeend position of #buddies approximates the append behavior:
const buddy_name = "Dale"
const new_buddy = `<li><a>${buddy_name}</a></li>`
const list = document.querySelector('#buddies')
list.insertAdjacentHTML('beforeend', new_buddy)
User-submitted HTML must never be inserted this way, since it opens the door to cross-site scripting vulnerabilities.
innerHTML
Query and content look like this:
const buddy_name = "Dale"
const new_buddy = `<li><a>${buddy_name}</a></li>`
const list = document.querySelector('#buddies')
list.innerHTML += new_buddy
The naive result appears correct:
<ul id="buddies">
<li><a>Alex</a></li>
<li><a>Barry</a></li>
<li><a>Clive</a></li>
<li><a>Dale</a></li>
</ul>
But the += in list.innerHTML += new_buddy is the trap. Because A += B means A = A + B, the existing HTML is copied and combined with the new mark-up. Event listeners cannot follow copies, so any click listener on the list's <a> tags is lost. That's why innerHTML isn't recommended for appending.
Summary and recommendations
A cheat sheet for the whole comparison:
| Method | Node | HTML Text | Text | Internet Explorer? | Event Listeners | Secure? | HTML Templating |
|---|---|---|---|---|---|---|---|
append | Yes | No | Yes | No | Preserves | Yes | Medium |
appendChild | Yes | No | No | Yes | Preserves | Yes | Medium |
insertAdjacentHTML | No | Yes | Yes1 | Yes | Preserves | Careful | Easy |
innerHTML2 | No | Yes | Yes | Yes | Loses | Careful | Easy |
insertAdjacentText is recommended.2 Instead of taking traditional parameters,
innerHTML is used like: element.innerHTML = 'HTML String'Condensed into a few rules:
innerHTMLis not recommended for appending because it removes event listeners.appendsuits those who want nodes or plain text and don't need Internet Explorer.appendChildsuits node-only work with full browser coverage.insertAdjacentHTMLsuits generated HTML where placement relative to the element matters.
Beyond the common four
These methods cover most use cases, but other append/insert options exist: before, after, insertBefore, and insertAdjacentElement.



