I’m using this code to play/pause background audio.
<div class="music-box">
<audio id="losAudio" src="images/honcayeu.mp3"></audio>
<button id="btn_playPause">
<i class="fa fa-music"></i>
</button>
</div>
var losAudio = document.getElementById("losAudio");
function losAudio_playPause() {
var isPaused = losAudio.paused;
losAudio[isPaused ? "play" : "pause"]();
}
document.getElementById("btn_playPause").addEventListener("click",losAudio_playPause);
But now i want when clicked on button, the icon will be change to fa-pause icon.
Can tell me a simple solution ? THanks
3
Answers
You need to do an action inside your function for that. Try to get the element (add an ID) and change the class of the i element
you can use
classList
,add()
andremove()
:the second way is that you can use
className
:To change the icon of the button from fa-music to fa-pause when the audio is playing, you can modify your losAudio_playPause function to update the button icon accordingly.
Try This :
What this does is it checks whether the audio is currently paused or playing, and sets the button icon to fa-music or fa-pause respectively using a ternary operator. The innerHTML property is then used to update the button’s content with the new icon.