So, When I press the button1 "1 PLAY / STOP", the button turns green and when I press it again it removes the color. If I press the button2 "2 PLAY / STOP" the same thing happens.
If one of the two buttons plays and I press the other, it removes the color of one and goes to the other.
I want to do the same with the Z button (button 1) and X (button 2). For example, when I press the Z button (keycode 90) or the X button (keycode 88) to do what it does with the mouse.
And if there is better coding in all this it would be very useful because it is more than 20 buttons... Thanks a lot!!!
$(document).ready(function(){
$('#btn1, #btn2').on('click', function() {
$('#btn1, #btn2').not(this).removeClass('colors');
$(this).toggleClass('colors');
});
});
var nowplaying = "";
function playButton1() {
var audio = document.getElementById("1");
play(audio);
}
document.addEventListener("keydown", function(e){
if (e.keyCode === 90){
var audio = document.getElementById("1");
play(audio);
}
});
function playButton2() {
var audio = document.getElementById("2");
play(audio);
}
document.addEventListener("keydown", function(e){
if (e.keyCode === 88){
var audio = document.getElementById("2");
play(audio);
}
});
function play(audio) {
if (audio != nowplaying){
if (nowplaying != "") {
nowplaying.pause();
}
audio.loop = true;
audio.play(audio);
nowplaying = audio;
} else {
audio.pause();
audio.currentTime = 0;
nowplaying = "";
}
}
#btn1, #btn2 {
padding:20px 30px
}
.colors {
background-color:green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="btn1" onclick="playButton1()">1 PLAY / STOP</button>
<button id="btn2" onclick="playButton2()">2 PLAY / STOP</button>
<audio id="1" src="http://www.sousound.com/music/healing/healing_01.mp3" preload="auto"></audio>
<audio id="2" src="http://www.sousound.com/music/healing/healing_01.mp3" preload="auto"></audio>