Multiple Events, Same Controller Action

Hi there!

I’m converting a bunch of old jQuery code into Stimulus code.

In jQuery, I’ve got something like:

$("#whole_amount").bind("focus change keyup input keypress", function(e){});

How would I do that in Stimulus? Do I have to repeat actions like so?

<input id="whole_amount" data-action="focus->fee#calculate change->fee#calculate keyup->fee#calculate input->fee#calculate">

Curious if there is a DRY way to do this…

Generally, yes, you need to explicitly list every event → action like that.

In your particular case, you can probably drop the keyup and change handlers since they’re both covered by input. And, depending on your implementation, you could try dropping the focus handler and call it on connect() instead. Then you’d be down to one action (input->fee#calculate). :slight_smile:

Brilliant, thank you!

I’m having a lot of fun moving to Stimulus. Thanks for building and sharing it.

2 Likes