Dwight Watson's blog

Using multiple actions on a single element with Stimulus

This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.

Sometimes it makes sense to want to fire an event from an element off to multiple controllers (if the element is a child of both controller scopes). Luckily this is something that was considered by the Stimulus team and it works out of the box.

<input type="email" name="email" data-action="email#change form#change" />

In this example, both EmailController#change and FormController#change would be called with the change event. There are many use-cases for this - in the above example your EmailController might be looking to validate that email with an external service while a FormController might be interested in toggling other relevant parts of the form for display.

A blog about Laravel & Rails by Dwight Watson;

Picture of Dwight Watson

Follow me on Twitter, or GitHub.