Let's say I have some code like this:
class MyElem extends HTMLElement {
constructor() {
super();
let templateContent = document.getElementById('template-elem').content;
this.innerHTML = templateContent.cloneNode(true);
}
}
window.customElements.define('my-elem', MyElem);
<template id="template-elem">
<div class="a">
<div class="b">b</div>
<div class="c">c</div>
</div>
</template>
<my-elem></my-elem>
Why doesn't this work? In the Chrome inspector, the custom element has no HTML inside of it. I've also tried doing:
this.append(templateContent.cloneNode(true));
but that also resulted in an empty HTML tree.
All the tutorials mention using the shadow DOM like the following:
this.attachShadow({mode: 'open'}).appendChild(templateContent.cloneNode(true));
and while that works, it forces you to use the Shadow DOM for your custom element. Is there no way of just appending the template's HTML to your custom element without being required to use the Shadow DOM? I'd prefer to just use global CSS styling in my small use-case.