It worked for me when I added {extends: 'div'}
to the options which is the third parameter to the define
method, so my code was like this
class MyCustomElement extends HTMLDivElement {
constructor(){
super()
}
connectedCallback() {
console.log("Custom element added to page.");
}
}
window.customElements.define('my-custom-element', MyCustomElement, {extends: 'div'})
// then I exported it and used it in another class
export default MyCustomElement;
and I used it like so
// another-file.js
import MyCustomElement from '../path/to/custom-element-class-file.js'
//...
const customDiv = new MyCustomElement()
document.body.append(customDiv)
//...
even the connectedCallback
method was called after the element got added to the DOM
customElements.define()
after defining its class - and much later the call todocument.createElement()
fails because of this exact error. – Shawannashawl