JavaScript Reference ยท DOM: Document and Elements
The template element
Reference for the HTML template element in JavaScript: read its content DocumentFragment, clone it with importNode or cloneNode and fill in repeated markup.
A <template> element holds markup that the browser parses but does not render. Its children are not part of the visible page, scripts inside do not run, and images do not load until you use the content. In JavaScript the markup is available as template.content, a DocumentFragment.
The usual pattern is to clone that fragment once per item, fill in the dynamic parts with textContent, and insert the clone. It keeps structure in HTML where it is easy to read, and avoids building markup from strings.
<template id="card"> <article class="card"><h3></h3><p></p></article> </template>
const clone = document.querySelector("#card").content.cloneNode(true);
Try it
Syntax
template.content // DocumentFragment template.content.cloneNode(true) // copy within the template's document document.importNode(template.content, true) // copy owned by the current document
Properties and methods used
| Name | Description |
|---|---|
template.content |
A read-only DocumentFragment that owns the template's parsed children. |
cloneNode(true) |
Deep copy of the fragment. Always pass true, or you get an empty fragment. |
document.importNode(node, true) |
Deep copy adopted into the current document. Equivalent for most uses. |
template.innerHTML |
Serializes and sets the template's markup (this reads and writes content). |
Return value
Cloning returns a new DocumentFragment. Inserting it moves its children into the page and leaves the fragment empty.
Usage notes
- The children live in
content, not in the template itself.template.childrenis empty; usetemplate.content.childrenortemplate.content.querySelector(). - Clone before you modify. If you insert
template.contentdirectly, you move the children out of the template and it is empty the second time. Always clone for each use. - Fill values with
textContent, notinnerHTML, whenever the data comes from a user or a server. It avoids injecting HTML. - Custom elements. Elements inside
template.contentbelong to a separate inert document, so custom elements are not upgraded until the clone is adopted into your page.importNode()or inserting the clone triggers this; both are fine in practice. - Scripts inside a template run only after insertion, like scripts added by DOM methods, but keep behavior in your main script instead.
- Table rows.
<template>can appear where other elements cannot, such as inside<table>or<tbody>, which makes it a good home for row markup. - Shadow DOM. A
<template shadowrootmode="open">is a declarative way to create a shadow root; see shadow DOM. - Event listeners are not cloned. Attach them to the clone, or use event delegation on the container.
Browser support
The template element and content are supported in all current browsers. Declarative shadow DOM (shadowrootmode) is more recent, so check a compatibility table.