<header>
  <h2 class="title">Document.importNode() Method</h2>
  <p class="description">Дозволяє додавати клоновані вузли до документа.</p>
</header>
<main>
  <template id="item-template">
    <div class="item">
      <p>Це клонований елемент</p>
      <button class="remove-btn">Видалити</button>
    </div>
  </template>
  <div class="result">
    <button id="add-btn">Додати елемент</button>
  </div>
</main>
body {
  font-size: 16px;
  line-height: 1.5;
  font-family: monospace;
}

header {
  background-color: #f1f1f1;
  margin-bottom: 25px;
  padding: 15px;
  box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
}

header h2.title {
  padding-bottom: 15px;
  border-bottom: 1px solid #999;
}

header p.description {
  font-style: italic;
  color: #222;
}

.result, .item {
  background-color: #f8f8f8;
  padding: 15px;
  box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
  margin-bottom: 10px;
}

.item p {
  margin: 0;
}

.remove-btn {
  margin-top: 10px;
}
document.getElementById('add-btn').addEventListener('click', function() {
  var template = document.getElementById('item-template').content;
  var clonedItem = document.importNode(template, true);
  
  clonedItem.querySelector('.remove-btn').addEventListener('click', function() {
    this.parentNode.remove();
  });

  document.querySelector('.result').appendChild(clonedItem);
});

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.