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" … Read more 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?

Source: JavaSript – Stack Overflow


Categories: NewsTags: ,

Tags: ,


Leave a Reply

Your email address will not be published. Required fields are marked *