user2225112
user2225112

Reputation: 77

Audio trigger in Javascript

I've coded up a quiz using javascript, I want to add sound effects when a user answers a question either correctly or incorrectly. Sounds will be different for correct and incorrect answers. Im just starting javascript and need this for a class project so any help would be greatly appreciated. I can upload the html and js code if anyone can help. I presume I have to use an onclick event with if or else statements but im not too sure how to do this. Thanks

Upvotes: 0

Views: 1690

Answers (1)

OpherV
OpherV

Reputation: 6937

   <audio id="right">
        <source src="right.mp3">
    </audio>
   <audio id="wrong">
        <source src="wrong.mp3">
    </audio>

Then to play:

document.getElementById("right").play();

or

document.getElementById("wrong").play();

Upvotes: 1

Related Questions