Skip to content
Advertisement

Adding an event within a WebComponent

I want to create a Web Component. I use a ShadowDom and after generating a list I want to add a click Event to each element of the list. But I wonder how to access the list.

Neither the document nor the template shows me the items after a querySelect.

My question: How can I access the generated list within the webcomponent?

JavaScript
JavaScript

Advertisement

Answer

You dont have access to the template over document. The template tag is a shadow DOM. You can make avaible for access the shadow DOM if you change the mode parameter to true: this.attachShadow({ mode: 'open' }). Then you can use this.shadowRoot.querySel.... Otherwise you can access directly over your shadow object (shadow = this.attachShadow({ mode: 'closed' })) with shadow.querySelector().

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement