I have a page with several images with a play button. When the play button is pressed, I try to swap the src sibling element using the path to the corresponding gif. For some reason, when I click the play button on the first item, it works fine. When I click the game on the second element, it changes src to the second gif, BUT also restarts the first gif. If I press the play button on the third play button, it restarts all 3 gifs from the very beginning.
HTML looks like this:
<div class="player">
<div class="player--image">
<img class="gif--control gif--play" src="/img/play-button.png" />
<img class="player--gif" src="/assets/player-image.png" data-gif="/assets/player-gif.gif"/>
</div>
<div class="player--info">
<div class="player--name">
<h3 class="title">#23</h3>
<h3 class="title title__large">Player Name</h3>
<h3 class="title">Center</h3>
</div>
<div class="excerpt">
Lorem ipsum
</div>
</div>
</div>
<div class="player">
<div class="player--image">
<img class="gif--control gif--play" src="/img/play-button.png" />
<img class="player--gif" src="/assets/player-2-image.png" data-gif="/assets/player-2-gif.gif"/>
</div>
<div class="player--info">
<div class="player--name">
<h3 class="title">#45</h3>
<h3 class="title title__large">Player Name</h3>
<h3 class="title">Center</h3>
</div>
<div class="excerpt">
Lorem ipsum
</div>
</div>
</div>
Javascript / jQuery looks like this:
$(document).on('click tap', '.gif--control', function(){
var $this = $(this);
var $gif = $this.siblings('.player--gif');
var src = $gif.attr('data-gif');
$gif.attr('src', src);
});
img, src , gifs . - , - .