function getAudio(player) { return player.getElementsByTagName("audio")[0]; } window.onload = function() { var players = document.getElementsByClassName("player"); for (var i = 0; i < players.length; i++) { getAudio(players[i])["queue"] = i; getAudio(players[i]).addEventListener('ended', function(event) { const next = event.target["queue"] + 1; if (next < players.length) getAudio(players[next]).play(); }); } }