# Mogelijk om event handlers aan widgets te koppelen in thema/plugin?

**URL:** <https://meta.discourse.org/t/possible-to-attach-event-handlers-to-widgets-in-theme-plugin/166204>\
**Category:** Development\
**Created:** [4 oktober 2020 om 15:42 UTC](https://meta.discourse.org/t/possible-to-attach-event-handlers-to-widgets-in-theme-plugin/166204 "2020-10-04T15:42:58Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![dchouinard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dchouinard/32/194662_2.png) [@dchouinard](https://meta.discourse.org/u/dchouinard)\
**Post date:** [4 oktober 2020 om 15:42 UTC](https://meta.discourse.org/t/possible-to-attach-event-handlers-to-widgets-in-theme-plugin/166204/1 "2020-10-04T15:42:58Z")

</div>

Hi,

I’m looking to add a click handler to items in the main topic list (to add custom behavior under some conditions). Is it possible to use something like `api.decorateWidget()` to add an event handler to every instance of a particular widget?

—

Other approaches considered:

- It’s not practical to use `addEventListener` in a global function because additional topic list items can be loaded after page load, e.g. infinite scroll. (the implementation would need to use something like ` MutationObserver` to observe changes in the DOM and attach more events handlers as more widgets render)
- It _could_ be possible to attach a click handler to `body` and monitor through event bubbling (e.g. as done through jQuery with `$("body").on("click", ".list-item-topic", function())`), but I need to be able to use `preventDefault()` and `stopPropagation()`, which I believe this approach doesn’t support

---

_[View the full topic](https://meta.discourse.org/t/possible-to-attach-event-handlers-to-widgets-in-theme-plugin/166204)._
