Add dynamic event listeners in LWC
Let's look at how to add event listeners dynamically in LWC templates.
• 1 min read
This is something you would need rarely but definitely worth knowing something like this is possible.
Let's look at how to add event listeners dynamically in LWC templates.
If the event is always a click event, then we can straight away use onclick
attribute on the template tags (we don't have to take this route).
In the renderedCallback
life cycle hook am trying to use the native JavaScript syntax to add the event listeners.
Here is the template file associated to the web component.
Hope this helps!