Solution 1 :

 <!doctype html>

          function playAudio() {
            var audio = document.getElementById("MySound");

        <input type="button" value="Play Audio" onclick="playAudio()">
        <audio id="MySound" src=""></audio>


Solution 2 :

Suppose you have a project with files like this:
Project files

Then you can do in your HTML & JS code like this to make the sound played randomly when the submit button is clicked:

//Load the sound files into an array
const audioArr = [
    new Audio('audio/audio1.ogg'),
    new Audio('audio/audio2.ogg'),
    new Audio('audio/audio3.ogg'),
    new Audio('audio/audio4.ogg'),
    new Audio('audio/audio5.ogg'),
    new Audio('audio/audio6.ogg')

function playRandomAudio(){
    //Get a random index of the sound to be played
    const randomAudioIndex = Math.floor(Math.random() * (audioArr.length+1));

    //Play the selected sound
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">

<input type="submit" value="Submit" onclick="playRandomAudio()">

<script src="test_sound.js"></script>

Hope this answers your question.

Problem :

I want to create a submit button, when every time you click it, it plays a random audio file.

Is there a fast way to do this with HTML/JS?


Comment posted by HelloWorld

Thanks but this is just to play 1 sound right, not a random sound from a list?

Comment posted by HelloWorld

Thanks I’ve tested this but only seems to play the first audio file, any ideas?