I have looked at a number of examples (eg this one) of this which don't work for me.
A simple Lit component. I would like to add an event listener on the button, selected by its id. The result in the console is null - it can't find the button in the shaddow Dom. What am I doing wrong?
import { html, LitElement } from "lit";
class MyElement extends LitElement {
connectedCallback() {
super.connectedCallback();
var buttonElement = this.shadowRoot.getElementById("myButton");
console.log(buttonElement);
}
render() {
return html` <button id="myButton">Click</button>`;
}
}
customElements.define("my-element", MyElement);
UPDATE:
Putting it here worked.
firstUpdated() {
var buttonElement= this.shadowRoot.getElementById("myButton");
console.log(buttonElement);
}