How to play music

How do I make it so a button plays music when you click on it?

In your script, add the code

let audio=new Audio("url to file")
document.getElementById("button id");

Can you be more specific? Because i tried putting it into the code different ways and filling out the red text with what’s actually supposed to be there but it isn’t working.

You might like remixing It is a button that picks one of two different airhorn sounds randomly and plays the one it picked.

const airhorns = [

const playAirhorn = () => {
  const audioFile = airhorns[Math.floor(Math.random()*airhorns.length)];
  const audio = new Audio(audioFile);;

Hope this helps,


1 Like