At work, I have a soundboard that I like to use to prank my coworkers. The soundboard has multiple buttons, each playing a different sound when clicked. Currently, I have to write a separate script for each button, which has led to 47 scripts on the page. I am looking for a better solution where I can have one script that works for all the buttons.
<button onclick="javascript:toggleSound1();">I don't know what we're yelling about.</button>
Here is an example of one of the buttons:
<audio id="sound1" src="sounds/dontknowwhatwereyellingabout.wav"></audio>
And the script for the button:
<script type="text/javascript">
function toggleSound1() {
var audioElem = document.getElementById('sound1');
if (audioElem.paused)
audioElem.play();
else
audioElem.pause();
audioElem.currentTime = 0;
}
</script>
It occurred to me that if I could somehow define the 'sound1' on button press, I could use the same script for all the buttons. Currently, I have to define 'sound1', 'sound2', 'sound3', and so on, and create a new 'toggleSound' function for each of them.