i’m making a music streaming website and i have this js function that plays the songs on a single player onclick. the songs are playing correctly but when i click on them the player thakes always the song name, the song artist and the song image of the first song, also if i click on the others. how can i fix that?
This is the js function:
function playSong(element){
var songUrl = element.getAttribute('data-song-url');
var audioPlayer = document.getElementById('audioPlayer');
var audioSource = document.getElementById('audioSource');
audioSource.src = songUrl;
audioPlayer.load();
audioPlayer.play();
var songId = element.id;
var newImage = document.getElementById("A_" + songId).src;
var newSongName = document.getElementById("B_" + songId).textContent;
var newArtistName = document.getElementById("C_" + songId).textContent;
console.log(newSongName) //always displaying the things of the first song
console.log(newImage)
console.log(newArtistName)
document.getElementById("audio-img").src = newImage;
document.getElementById("audio-song").textContent = newSongName;
document.getElementById("audio-artist").textContent = newArtistName;
}
and this is the HTML code:
{% for i in songs %}
<div class="carousel-cell">
<section class="main_song">
<div class="song-card">
<div class="containera">
<img src="{{i.image}}" id="A_{{i.id}}" alt="song cover">
<div class="overlaya"></div>
<div>
<a class="play-btn" data-song-url="{{i.song.url}}" id="{{i.id}}" onclick="playSong(this)"><i class="fas fa-play-circle fa-2x"></i></a>
{% if user.is_authenticated %}
<div class="add-playlist-btn">
<a id="W_{{i.song_id}}" title="Add to Playlist" onclick="showDiv(this)"><i class="fas fa-ellipsis-h"></i></a>
</div>
{% endif %}
</div>
</div>
</div>
<div>
<p class="songName" id="B_{{i.id}}">{{i.name}}</p>
<p class="artistName" id="C_{{i.id}}">{{i.artist}}</p>
</div>
</section>
</div>
{% endfor %}