Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

283
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda