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

136
Visualizações
Play sound on click ( Anywhere)

Im trying to create a function that when you click down anywhere on the page a sound is played. I can only find information on how to do this for buttons. If you can help me that would be amazing

html css and js

Thank you

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

This script binds an click event to any element in the body, upon click will play the sound stated in the audio tag.

<!doctype html>
<html>
  <head>
    <title>Play audio</title>
  </head>
  <body onload="addEvent()">

    <script>
      function addEvent(){
         document.body.addEventListener("click", play)
       }
      function play() {
        var audio = document.getElementById("audio");
        audio.play();
      }
    </script>
    <h1>click any element on the body to play sound </h1>
    <audio id="audio" src="https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3"></audio>

  </body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

Why dont just use onClick event on the body

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body onclick="playAudio()" style="height: 1000px;"> 
<p>CLICK</p>

    <audio id="audio" src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3"></audio>

<script>
  
function playAudio() {
 var audio = document.getElementById("audio");
 audio.play();

}
</script>    
</body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

you should just handle onclick event.

<html lang="en">

<head>
    <meta charset="UTF-8">
</head>
<body>
</body>
<script>
    var should_play=true
    window.onclick = () => {
        if (should_play){
            should_play=!should_play
            let soundSource = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3";
            let sound = new Audio(soundSource);
            sound.play();
            sound.onended = () => {
                should_play=true
            }
        }

    }

</script>

</html>

variable should_play has been defined for not to playing song again when it is playing.

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