Dofactory.com
Dofactory.com
Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

HTML <template> id Attribute

An id on a <template> tag assigns an identifier to the template.

The identifier must be unique across the page.

Example

#

An id attribute on a <template> element.
Each time the button is clicked an item is added to the list.

  • Item 1

<template id="item-template">
  <li></li>
</template>

<ul id="list">
  <li>Item 1</li>
</ul>

<button onclick="add();">Add List Item</button> <br />

<script>
  let itemCount = 1;

  let add = () => {
     itemCount++;
  
     let template = document.getElementById("item-template");  
     let content = template.content;
     content.querySelector('li').textContent = "Item " + itemCount;
  
     let row = template.content.cloneNode(true);

     document.getElementById("list").appendChild(row);
  }
</script>

Using id

The id attribute assigns an identifier to the <template> element.

The id allows JavaScript to easily access the <template> element.

It is also used to point to a specific id selector in a style sheet.

Tip:  id is a global attribute that can be applied to any HTML element.


Syntax

<template id="identifier" />

Values

#

Value Description
identifier A unique alphanumeric string. The id value must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens (-), underscores (_), colons (:), and periods (.).

More Examples

A <template> with a unique id.
Each time the button is clicked a new row (i.e. template row) is added to the table.

ID Customer Name
1
<style>
  table.tb { width:300px; border-collapse: collapse; }
  .tb th, .tb td { border: solid 1px #777; padding: 5px; }
  .tb th { background: lightblue; }
</style>

<template id="mytemplate">
  <tr>
    <td><span></span></td>
   <td><input type="text" name="name[]" value=""></td>
  </tr>
</template>

<table id="user-table" class="tb">
 <tr>
  <th>ID</th>
  <th>Customer Name</th>
 </tr>
  <tr>
    <td><span>1</span></td>
   <td><input type="text" name="name[]" value="Anna Kroger"></td>
  </tr>
</table>

<button onclick="addRow();">Add Customer</button>

<script>
  var counter = 1;

  let addRow = () => {
     counter++;
  
     let template = document.getElementById("mytemplate");  
     let content = template.content;
     content.querySelector('span').textContent = counter;
  
     let row = template.content.cloneNode(true);

     document.getElementById("user-table").appendChild(row);
  }
</script>

Code explanation

The id attribute assigns a unique identifier for the <template>.

When the button is clicked, JavaScript locates the <template> through the id.

It extracts the <template> content and add it as the last child of the table.


Browser support

Here is when id support started for each browser:

Chrome
26.0 Mar 2013
Firefox
22.0 Jun 2013
IE/Edge
13.0 Nov 2015
Opera
15.0 Jul 2015
Safari
9.0 Sep 2015

You may also like

 Back to <template>

Last updated on Sep 30, 2023

Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

Guides