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

275
Visualizações
How to correctly clone content of <template> in JS

I am creating simple “to do list” by Java Script. I have template for tasks, which content I would like to clone, and then I am trying to insert task’s text inside this content.

<template id="li-temp">
    <li class="todo-item__undone"></li>
    <button class="delete-item-button delete-item-button-not-displayed">&#10008;</button>
</template>

A method, which try to clone template's content and insert task's content:

createTaskHtml(content, status, id){
    let template = document.getElementById("li-temp").content.cloneNode(true);
    let li = template.firstChild;
    let li_test = document.createElement("li")
    li.id = id;
    li.innerText = content;
    let deleteButton = template.lastChild;
    if (status === "done"){
        li.classList.add("todo-item__done");
        deleteButton.classList.remove("delete-item-button-not-displayed");
        this.setListener(deleteButton);
    }
    return li;
}

I used "content" to get template's content, then I used method "cloneNode(true)" for deep cloning. I am expecting to get node with two children (li and button), so I am truing to get object li as firstChild. But then I am getting an error becouse object li doesn't have attribute "id" and "innerText".

I debuged this code and was surprised to find that WebStorm identified this node as text node. So I assume that I am somehow copying the content of the template incorrectly, but, of course, I admit that the error may be in something else

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

0

Return template variable not li

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