---
title: "Comment by William Owen on \"How To Create Reusable HTML Components Using Only JavaScript\" | Hashnode"
description: "You mentioned a way to assign an event to a button within a slot, and mentioned how the event doesn't get added to slot content that is injected... Using delega"
url: "https://hashnode.com/posts/create-reusable-html-components-with-javascript/64c2ddc261b710e7e3bb8ec2/comment/6711a0ca83b2ac1440eea0bd"
---

[Discussion](https://hashnode.com/posts/create-reusable-html-components-with-javascript/64c2ddc261b710e7e3bb8ec2)

Comment [How To Create Reusable HTML Components Using Only JavaScript](https://hashnode.com/posts/create-reusable-html-components-with-javascript/64c2ddc261b710e7e3bb8ec2)

[William Owen](https://hashnode.com/@williamowendev)

[Oct 17, 2024](https://hashnode.com/posts/create-reusable-html-components-with-javascript/64c2ddc261b710e7e3bb8ec2/comment/6711a0ca83b2ac1440eea0bd)

You mentioned a way to assign an event to a button within a slot, and mentioned how the event doesn't get added to slot content that is injected... Using delegated event handling, you can get around that. The basic idea is that the click event is on the shadow dom, and in the event handler we check if the button was clicked.

connectedCallback() { this.shadowRoot.addEventListener('click', (e) => { if(e.target.closest('.primary-btn')){ console.log('shadow event triggered!') } }) }
