I have a trio of play buttons for a custom player setup, displayed like so:
<div>
<div class="gs-player">
<div id="gs1" onclick="play(309689093)" class="fa fa-3x fa-play"></div>
</div>
<div class="gs-player">
<div id="gs2" onclick="play(316017522)" class="fa fa-3x fa-play"></div>
</div>
<div class="gs-player">
<div id="gs3" onclick="play(315363199)" class="fa fa-3x fa-play"></div>
</div>
</div>
My goal is to achieve the following actions when any of them are clicked:
- Remove the active class (identified by
fa-pause
) from all buttons - Toggle the
fa-pause
class on the clickeddiv
This is how I tried to accomplish it:
$(document).ready(function() {
$('.fa-play').click(function () {
$('.fa-pause').removeClass('fa-pause');
$(this).toggleClass('fa-pause');
});
});
Although it works as intended, there seems to be an issue with the toggle function. It behaves more like an addClass rather than toggling the class off if the same div
is clicked again.
You can see the behavior more clearly in this JSFiddle.
Additionally, is there a way to achieve this without relying on a framework?