I have made a soundboard website which has a number of small audio clips and the below Javascript to play, pause and reset them when they are finished playing. The page works exactly as expected but I get a
index.html:90 Uncaught TypeError: Cannot read property 'getAttribute' of null
error in my Javascript console and I have no idea why.
cliplist = ['audio1','audio2','audio3','audio4','audio5','audio6','audio7','audio8'];
playerlist = ['player1','player2','player3','player4','player5','player6','player7','player8'];
document.addEventListener("DOMContentLoaded", function()
{
for(let i=0;i<cliplist.length;i++)
{
document.getElementById(playerlist[i]).addEventListener("click", function()
{
playClip(cliplist[i]);
})
document.getElementById(playerlist[i]).addEventListener("click", function()
{
playClip(cliplist[i],playerlist[i]);
})
document.getElementById(cliplist[i]).addEventListener("ended", function()
{
reset(playerlist[i]);
})
}
});
function playClip(theclip,theplayer)
{
playerElement = document.getElementById(theplayer);
clipElement = document.getElementById(theclip)
if(playerElement.getAttribute("src") == 'playbutton.png')
{
playerElement.setAttribute("src","pausebutton.png");
clipElement.play();
}
else
{
playerElement.setAttribute("src","playbutton.png");
clipElement.pause();
}
}
function reset(theplayer)
{
document.getElementById(theplayer).src = 'playbutton.png';
}
document.getElementById(theplayer);check all the elements to see if they are there['player1','player2','player3','player4','player5','player6','player7','player8']