Jack
Jack

Reputation: 35

HTMLElement is not added to the DOM. Why?

I have the separate template file (html). I need to load this generated HTML object to the main page several times, so I do the next thing:

const path = "/templates/news.tpl.html";
const template = await Dbc.Dom.loadTemplate({ 
    id  : "news-block", 
    path: path 
});

document.body.appendChild(template);
document.body.appendChild(template);

First, the function fetches the content from the file and add it to the current DOM. As the return value, it returns the added HTMLElement object.

I try to add this object again after that, then I'm using the standard way, via the document.body.appendChild() function, but I can see only the single template object in the current DOM.

Upvotes: 0

Views: 75

Answers (1)

Anton Gladchenko
Anton Gladchenko

Reputation: 46

Append child just replaces your object , if u try few times your object will apear in a last place. You should use cloneNode

sort of this :

    document.body.appendChild(template);
document.body.appendChild(template.cloneNode(true));

Upvotes: 2

Related Questions