How do I make a button submit a form and play a sound

Asked

Viewed 91 times

1

I’m making a web application that when the person clicks the button submit the form and automatically is playing a warning sound, but I’m not able to reproduce the sound and submit the form ,it only performs one function ignoring the other.

<button type="submit" class="btn btn-block btn-primary" onClick="$('#audio')[0].play();$('#chamarPainel').submit();"> Chamar </button>

1 answer

0


A good approach is to use setTimeout to submit the form after a certain time (after playing the audio, for example).

You can do something like this:

const audio = new Audio('https://lffg-archive.github.io/sopt/for-sure.mp3');
const form = document.querySelector('form');

form.addEventListener('submit', (event) => {
  // Previne a submissão original do formulário:
  event.preventDefault();

  // Toca o áudio:
  audio.play();

  setTimeout(() => {
    // Após 1 segundo (1000 milissegundos), faça a submissão do formulário:
    form.submit();
  }, 1000);
});
<form action="/action" method="post">
  <button type="submit">Submeter</button>
</form>

  • It worked ,VLW.

  • Man, now it’s not working out ,he’s ignoring play and he’s doing Submit

Browser other questions tagged

You are not signed in. Login or sign up in order to post.