I'm trying to use jQuery to add class "active" to a link when clicked, then auto-remove after 6 seconds. If link has class "active" when clicked, then go ahead and remove class. I am also trying to this link to play/stop play of mp3.
HTML:
<audio id="cat-growl-sound" controls>
<source src="sound.mp3" type="audio/mpeg">
</audio>
<a id="cat-growl-link" href="javascript:void(0)">Fire Me!</a>
CSS:
audio { display: none; }
#cat-growl-link.active { color: red; }
JQUERY:
$('#cat-growl-link').click(function() {
$("#cat-growl-sound")[0].play();
$(this).addClass('active');
$(this).removeClass('active').delay(6000);
});
$('#cat-growl-link.active').click(function() {
$("#cat-growl-sound").stop();
$(this).removeClass('active');
});
I have tried different methods to try to get this working, but the different methods only get parts of it working, not all of it. This script I chose to post here I think best explains what I am attempting to do, even though it doesn't work. How do I get this working properly?
Here is a jsFiddle with working sound: https://jsfiddle.net/tcmpguok/