I know that in the ‘data-action’ attrib are avaible: click, submit, change
But is there avaible methods like onKeyPress?
I know that in the ‘data-action’ attrib are avaible: click, submit, change
But is there avaible methods like onKeyPress?
You can handle any DOM event with a Stimulus action. Here’s the HTML for a text input with a keypress action:
<input type="text" data-action="keypress->foo#bar">
The value of the input element’s data-action attribute tells Stimulus to handle keypress events for the input by invoking the bar method in the foo controller. Read more about action descriptors in the Handbook.
Hi,
If you want to fetch “keypress” on the whole page, like a short cut to play next song, then
you could use the following action:
<div data-controller="my-controller" data-action="keypress@window->my-controller#keypress">
...
</div>
Cordialement,