@samstickland Why wouldn’t it be tenable to rewrite to Stimulus? It sounds like your JS is structured in such a way - DOM manipulation of nodes created after DOMContentLoaded
- that Stimulus would be a match made in heaven. Sure, every migration to new tech takes time and testing, but you need to fix your javascript anyway. It’s also very easy to implement - you can probably keep most of your code and wrap it in the connect() { JS CODE HERE }
method on a per-controller basis. Just add a data-controller="dropdown"
to the DOM node in question, and put your code in a dropdown_controller.js
and you won’t need an event at all.
If you really don’t want to add Stimulus to your project for this, it’s not like Stimulus is doing anything truly magic here. You can observe the relevant part of the DOM with your own Mutation Observer code and run the code on newly created elements when they appear in the DOM. Here’s a nice vanilla JS example I found through twenty seconds of googling. This is similar to what Stimulus is doing under the hood.
(Also, nitpick: you say you’re implementing Hotwire, but Hotwire includes Stimulus. You probably mean Turbo.)
@n-studio One solution is using Mutation Observers directly, see above.