I want to get the id of the pause button along with the play button id when the play button is clicked? As I want multiple play button. so I need a function that could serve all buttons. Here I have to first fetch play button id then pause button id. Also i have to write id of pause button like document.getElementById('pausebutton'); I want to fetch its Id in function too. I am not able to get both button ids in one function.
document.getElementById('pausebutton');
<!DOCTYPE html> <html lang="en-US"> <head> <title>Page Title</title> <meta charset="UTF-8"> <meta name="description" content="Free Web tutorials"> <meta name="keywords" content="HTML, CSS, JavaScript"> <meta name="author" content="John Doe"> <meta http-equiv="refresh" content="30"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> </head> <body> <i class="fa fa-play" style="font-size:24px;" id="playbutton" onclick="togglePlay('play',document.getElementById('myAudio'),this.id)"></i> <i class="fa fa-pause" style="font-size:24px;display:none;" id="pausebutton" onclick="togglePause('pause',document.getElementById('myAudio'),this.id)"></i> <a href="http://localhost/bkportfolio/wp-content/uploads/2021/01/ENG_US_M_DaveL.mp3" download="ENG_US_M_DaveL.mp3"><button>Download</button></a> <audio id="myAudio"> <source src="http://localhost/bkportfolio/wp-content/uploads/2021/01/ENG_US_M_DaveL.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio> <script> function togglePlay(state,aid,playid) { var p=playid; var audio = aid, play = document.getElementById(p), pause = document.getElementById('pausebutton'); if (state == 'play') { audio.play(); play.style.display = 'none'; pause.style.display = 'block'; } } function togglePause(state,aid,pauseid) { var p=pauseid; var audio = aid, pause = document.getElementById(p), play = document.getElementById('playbutton'); if (state == 'pause') { audio.pause(); play.style.display = 'block'; pause.style.display = 'none'; } } </script> </body> </html>
Get the pause.id in the togglePlay function when you're getting the play.id. let pauseId = play.nextSibling.id
pause.id
togglePlay
play.id
let pauseId = play.nextSibling.id
1.4m articles
1.4m replys
5 comments
57.0k users