I have a custom element that displays a message inside three nested divs:
class HelloWorld extends HTMLElement {
connectedCallback() {
const div1 = document.createElement('div')
this.appendChild(div1)
const div2 = document.createElement('div')
div1.appendChild(div2)
const div3 = document.createElement('div')
div3.textContent = 'Hello World!';
div2.appendChild(div3)
}
}
customElements.define('hello-world', HelloWorld)
It works, but I'm not happy with having all of the HTML markup inside of strings. I want to separate the HTML markup from the JavaScript, preferably in a separate file so that my IDE can give me better support. Here's an example of how I want to separate them:
<div>
<div>
<div id="inner-div"></div>
</div>
</div>
class HelloWorld extends HTMLElement {
connectedCallback() {
const innerDiv = this.getElementById('inner-div')
innerDiv .textContent = 'Hello World!';
}
}
customElements.define('hello-world', HelloWorld)
Is it possible to separate them like this?