HOWTO · JavaScript

How to Append HTML Content to the Existing HTML Element Using JavaScript

This tutorial details how to append HTML using JavaScript.

On this page

To add content to an existing element, choose the API based on what you have: use appendChild() to add a DOM node, or insertAdjacentHTML() to parse and insert an HTML string at a chosen position. Assigning to innerHTML replaces the element’s descendants, so it is not the way to preserve its existing children.

Drawback of Using innerHTML to Add a New HTML Node

Setting an element’s innerHTML parses the assigned string and replaces that element’s descendant nodes. This can discard state held by those nodes and event listeners attached directly to them. It does not mean that every attribute on the target element is removed: the target itself remains, while its children are replaced.

Both innerHTML and insertAdjacentHTML() parse strings as markup. If a string includes untrusted input, it can create a cross-site scripting (XSS) risk. Prefer creating nodes and assigning user-provided values through text properties such as textContent; if markup must be inserted, sanitize it with an approach suitable for the application. See MDN’s innerHTML security considerations.

Adding HTML Content Without Replacing the DOM Elements

For a newly constructed element, create a node and insert it into the existing tree. For a trusted HTML string, use insertAdjacentHTML() and select the insertion point. These approaches add content without replacing the target’s existing descendants. The examples below use a small address form to show both choices.

Using appendChild() to Append HTML Content in JavaScript

appendChild() accepts a node, not an HTML string. It places that node after the target’s current last child. If the node already belongs to another part of the document, appending it moves the same node rather than creating a copy.

Run this example:

function addAddressLine(addressContainer, lineCount) {
  const input = document.createElement("input");
  input.type = "text";
  input.placeholder = `Address Line ${lineCount}`;
  addressContainer.appendChild(input);
}

if (typeof document !== "undefined") {
  let lineCount = 0;
  const button = document.getElementById("add-address");
  const addressContainer = document.getElementById("address");

  button.addEventListener("click", () => {
    addAddressLine(addressContainer, ++lineCount);
  });
}

Place the script after this form, or load it with defer, so its elements exist before it looks them up:

<form>
  <label>Name: <input id="name" type="text"></label>
  <div id="address">
    <label>Address: <input id="contact" type="text"></label>
  </div>
  <button id="add-address" type="button">Add Address Line</button>
</form>

The click handler increments the line number and appends a new text input to the address element. The original name and address fields remain in place. The type="button" attribute prevents the button from submitting the form.

Note

append() is another option: it accepts nodes and string text, but a string argument is inserted as text rather than parsed as HTML. Use appendChild() when its returned node is useful or when the code needs the established single-node operation. Browser support is not discussed here; check the target browsers for an application-specific requirement.

Using insertAdjacentHTML() to Insert HTML at a Specific Position

Call target.insertAdjacentHTML(position, htmlString) to parse a string and insert the resulting markup relative to target:

  • beforebegin inserts immediately before the target element. The target needs a parent.
  • afterbegin inserts inside the target, before its first child.
  • beforeend inserts inside the target, after its last child. This is the position used when appending markup.
  • afterend inserts immediately after the target element. The target needs a parent.

For example, this inserts a new address input at the end of the address container:

if (typeof document !== "undefined") {
  const addressContainer = document.getElementById("address");
  addressContainer.insertAdjacentHTML(
    "beforeend",
    '<input type="text" placeholder="Additional address line">'
  );
}

Unlike appendChild(), this method parses the second argument as HTML. Use it only when the string is trusted or has been safely sanitized; do not concatenate untrusted text into markup. For user-entered content, create an element and set its textContent or other appropriate properties, then append the node.