I get an issue using custom elements.
Error : Uncaught DOMException: Failed to construct 'CustomElement': The result must not have children
'use strict';
class TestCard extends HTMLDivElement {
constructor() {
super();
this.headerNode = document.createElement('div');
this.bodyNode = document.createElement('div');
this.headerNode.className = 'card__header';
this.bodyNode.className = 'card__body';
this.appendChild(this.headerNode);
this.appendChild(this.bodyNode);
}
connectedCallback() {
this.classList.add('card');
}
static get observedAttributes() {
return ['data-header', 'data-body'];
}
attributeChangedCallback(attrName, oldValue, newValue) {
if (newValue !== oldValue) {
this[attrName.replace('data-', '')] = newValue;
}
}
set header(value) {
this.headerNode.textContent = value;
this.setAttribute('data-header', value);
}
set body(value) {
this.bodyNode.innerHTML = value;
this.setAttribute('data-body', value);
}
}
customElements.define('test-card', TestCard, {
extends: 'div'
});
<div is="test-card" data-header="Title" data-body="Content"></div>
Creating the WebComponent :
var cardNode = document.createElement('div');
cardNode.setAttribute('is', 'test-card');
cardNode.header = header;
cardNode.body = body;