Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

286
Views
How to stop an audio element created by a function? IE: audio = new Audio()

I'm creating a Canvas game and I'm using this to stop all audios:

  document.querySelectorAll("audio").forEach((el) => {
  if (
    el.id === "song1" ||
    el.id === "song2" ||
    el.id === "song3"
  ) {
    el.volume = 0;
    el.pause();
  } else {
    el.currentTime = 0;
    el.volume = 0;
    el.pause();
  }
});

The problem I have is that this stop all the audios within the <head></head> tag in the html file (preloaded ones), but it does not work for audios created dinamically

function soundGo(newAudio, boolean) {
  if (boolean === true) {
    let audio = new Audio(newAudio);
    audio.volume = 0.1;
    audio.play();
} else {
    let audio = new Audio(newAudio);
    audio.volume = 0;
    audio.pause();
}
}

I want to know if it's possible to iterate all the elements created with new Audio(). The querySelectorAll does not the trick, at least in my case (maybe I'm doing it wrong...).

I tried this also: stop dynamically created Audio

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!