Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

HTMLTemplateElement.content: the inert template fragment

`template.content` exposes a template’s inert contents as a `DocumentFragment`. To reuse the template, copy its subtree into the destination document and insert the copy.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

template.content

content is a read-only instance property of HTMLTemplateElement: it returns the same DocumentFragment each time, but you can read and modify the nodes in it. Markup inside an ordinary template element is parsed into that fragment, not as children of the element itself. To find nodes in the contents, use template.content.querySelector(...), not template.querySelector(...).

In a normal HTML document, the fragment’s ownerDocument is a separate document with no browsing context. If the template already belongs to a document created to hold template contents, the fragment reuses that document. Connecting the template to the page does not render its contents or run scripts that are still in the fragment. Inertness is not sanitization: untrusted HTML can still be dangerous when inserted. Use textContent for plain text.

To keep the template reusable, call document.importNode(template.content, true) on the destination document, then insert the copy. true also copies the descendants; omitting the flag or passing false gives you an empty copy of the fragment. Unlike cloneNode(true), importing uses the destination document’s context, which matters for the custom element registry. Inserting a fragment moves its children, not the fragment itself, and leaves it empty. If you insert the original, you empty the template.

This is an HTML DOM API, not part of ECMAScript 2024. JavaScript support alone does not guarantee that the API is available.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Template content example</title>
</head>
<body>
  <script>
    (() => {
      const template = document.createElement('template');
      const paragraph = document.createElement('p');
      paragraph.textContent = 'Hello';

      const original = template.content;
      original.appendChild(paragraph);

      const host = document.createElement('div');
      host.appendChild(template);
      document.body.appendChild(host);

      const copy = document.importNode(original, true);
      const inserted = copy.firstElementChild;
      host.appendChild(copy);

      console.log(`template children: ${template.childNodes.length}`);
      console.log(`original children: ${original.childNodes.length}`);
      console.log(`inserted: ${inserted.isConnected}, ${inserted.textContent}`);
      console.log(`copy children: ${copy.childNodes.length}`);
      host.remove();
    })();
  </script>
</body>
</html>

Result

template children: 0
original children: 1
inserted: true, Hello
copy children: 0

Open the HTML file in a browser and check the developer tools console. The template has no direct children, the original fragment keeps its paragraph, the inserted paragraph copy is connected to the document, and the copied fragment is empty.

Related lessons

  • JavaScript · Lesson 16 · Advanced · Version: 4

Sources