Forum Discussion
How to play audio files by hovering over text
While you can do what you asked entirely in JavaScript, it would be cumbersome to set up and maintain across many slides and is probably not the best approach. All you really need to do is use the Storyline click and hover triggers to play your desired audio clips. You can set the audio to automatically stop upon hovering out, or you could use the script shown below to stop all playing audio on your slide. The script could be useful if, as you say, you want to stop an arbitrary previously playing audio before starting a new one.
const audioList = document.querySelectorAll("audio");
for (let i = 0; i < audioList.length; i++) {
audioList[i].pause();
}
I would create one such Execute JavaScript trigger set to run when a true/false variable changes. Then, for each click or hover event you want to play a new audio, toggle this variable before starting the new clip.
Note: A main problem with the script you generated is that it is trying to use the SL text labels as IDs to find the elements. The objects in SL generally have random character strings as IDs, or sometimes no IDs. You have to examine your slide content in the browser inspector before deciding how you will go about identifying the parts.