Skip to content

Music player playlist logic

I’m creating a small music player web app, everything works fine, I have a problem:

I want to create a playlist for it too and I’m kind of beginner in JS and I don’t know how to create the logic for that (every time user click on a specific song title in playlist, play that exact song which he just clicked)I stored songs in a Array

Source code:



You can implement this behaviour in different ways.

This is what I tried:

I added a function called setSong() which accepts a parameter called number and sets the song to that number (code logic copied from prevSong() and nextSong()). I added to every HTML element with the class .player__song the onclick attribute with the value of setSong(<index>) where <index> is the index of the song in the array.

The setSong() function looks like this:

function setSong(number){
  currentSong = number