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

279
Views
How to use same Javascript function for different id's
function togglePlay() {
  var myAudio = document.getElementById("myAudio");
  return myAudio.paused ? myAudio.play() : myAudio.pause();
};
<audio id="myAudio1" src="https://clubajax.pythonanywhere.com/media/contact/backgroundM.mp3" preload="auto"></audio>

<input type="button" value="sound" onclick="togglePlay()" />

<audio id="myAudio" src="https://clubajax.pythonanywhere.com/media/contact/backgroundM.mp3" preload="auto"></audio>

<input type="button" value="sound" onclick="togglePlay()" />'

I want to use this tooglePlay() function for different ID's, so that I can pause and play the independently for each song. How can I pass the different audio id's to same function?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can make the function accept the id as a parameter:

function togglePlay(id) {
  var myAudio = document.getElementById(id);
  return myAudio.paused ? myAudio.play() : myAudio.pause();
};

Then, in your HTML, pass the id of the corresponding audio tag when calling togglePlay:

<audio id="myAudio1" src="https://clubajax.pythonanywhere.com/media/contact/backgroundM.mp3" preload="auto"></audio>

<input type="button" value="sound" onclick="togglePlay('myAudio1')" />

<audio id="myAudio" src="https://clubajax.pythonanywhere.com/media/contact/backgroundM.mp3" preload="auto"></audio>

<input type="button" value="sound" onclick="togglePlay('myAudio')" />'
about 4 years ago · Juan Pablo Isaza Report
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!