Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
Appending template.content.cloneNode(true) multiple times not working

This does not work:

let template = document.getElementById("template");
const templateContentCloned = template.content.cloneNode(true);
document.getElementById("div1").appendChild(templateContentCloned);
document.getElementById("div2").appendChild(templateContentCloned);
document.getElementById("div3").appendChild(templateContentCloned);
<template id="template">
  <p>Here is my paragraph.</p>
</template>

<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>

But this does:

let template = document.getElementById("template");
const templateContent = template.content;
document.getElementById("div1").appendChild(templateContent.cloneNode(true));
document.getElementById("div2").appendChild(templateContent.cloneNode(true));
document.getElementById("div3").appendChild(templateContent.cloneNode(true));
<template id="template">
  <p>Here is my paragraph.</p>
</template>

<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>

Why? Why do you have to clone the node every time you want to use the template?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

template.content contains a documentFragment it can only be inserted into DOM once:

A common use for DocumentFragment is to create one, assemble a DOM subtree within it, then append or insert the fragment into the DOM using Node interface methods such as appendChild() or insertBefore(). Doing this moves the fragment's nodes into the DOM, leaving behind an empty DocumentFragment

So after it was inserted into div1 variable templateContentCloned become an empty documentFragment hens nothing inserted into div2 and div3

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda