Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

93
Visualizações
Can't seem to get Javascript to perform onclick audio

I'm trying to create an onclick soundboard but I can't seem to get anything to work. I'm new to javascript. I also am unsure if my audio files are actually being linked in the function because VSCode is not even attempting to autofill the file location. the mp3s are located in the project file inside of the assets folder.

<body>
<ul>
<li onclick="playBeat1()"></li>
</ul>
<script type="text/javascript" src="js/main.js"></script>
</body>

///JS FILE///

function playBeat1() {
    var audio = new Audio("tone1.mp3");
    audio.play();
  }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

(can't comment) well for starters you need to show the code where your playBeat1() function is being called so that we can help you further. But if that function is the only thing in your code then you need to run the function when an event listener is fired. For example:

document.getElementById("elementNameHere").addEventListener("clicK",function() {
   playBeat1();
});

But please note that your audio will not play right away. The user needs to click something before you can start playing audio with javascript. To get around this just place a start button that will reveal the rest of the page.

Also, I noticed your html file is sort of...not right. You need to wright:

<!DOCTYPE html>
<html>
  <head>
    <title>title here</title>
  </head>
  <!-- body element here -->
</html>

(just noticed your js file location) assuming that only js files go into the "js" folder, you need to write:

var audio = new Audio("../audioFolder/tone1.mp3");

the ../ will give you access to the folder that your js folder is inside of.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda