Skip to content
Advertisement

Play video on hover only working on the first element

When the mouse hovers over a div a video will play. Everything works fine but the video only plays on the first “player”. I can see the thumbnail on every player but they don’t start to play. Why is this happening?

JavaScript
JavaScript

Advertisement

Answer

The issue is because you only retrieve the first item in the collection of elements (ie. the [0] item) and bind events to that. None of the other elements are affected. To fix this you need to loop through them.

I would also suggest using addEventListener() for this instead of the onX event properties. Try this:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement