Stimulus: How can I handle mouseover mouseout events with a single data-action?

Created on 10 Jan 2018  路  2Comments  路  Source: hotwired/stimulus

Hi, if I put two data-action on same HTML node, first one is not recognized.
For handle a mouseover mouseout event I need to build two different nodes.
Is there another way?

This code work but I don't know if is the best solution:

<div data-controller='monkey' >
  <div data-action='mouseover-monkey#mouseOver'>
    <div data-action='mouseout-monkey#mouseOut'>
      <img src="imageOver.png" ></img>
    </div>
  </div>
</div>

Thanks!

Most helpful comment

First, make sure JavaScript is the appropriate tool for the job. Can you do what you want with CSS instead?

If you decide you do want to use Stimulus, you can specify multiple actions in the same data-action attribute by separating them with a space:

<div data-action="mouseover->monkey#mouseOver mouseout->monkey#mouseOut">

Note that mouseOver and mouseOut are poor choices for your action method names. Repeating the event name won鈥檛 help a reader of the HTML understand what will happen when you mouse over or out of the element. Name your action methods by what will happen, not what triggers them.

All 2 comments

First, make sure JavaScript is the appropriate tool for the job. Can you do what you want with CSS instead?

If you decide you do want to use Stimulus, you can specify multiple actions in the same data-action attribute by separating them with a space:

<div data-action="mouseover->monkey#mouseOver mouseout->monkey#mouseOut">

Note that mouseOver and mouseOut are poor choices for your action method names. Repeating the event name won鈥檛 help a reader of the HTML understand what will happen when you mouse over or out of the element. Name your action methods by what will happen, not what triggers them.

CSS in my case was the best solution but I was curious 馃槃
Thanks for the suggestion and advise! 馃帀

Was this page helpful?
0 / 5 - 0 ratings

Related issues

incompletude picture incompletude  路  3Comments

savroff picture savroff  路  4Comments

merwok picture merwok  路  4Comments

chocnut picture chocnut  路  4Comments

angarc picture angarc  路  3Comments