Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get the next element of an array in javascript?
    primarykey
    data
    text
    <p>I am making my own music player in javascript and am working on the next song button. I have it set up to where the songs get added to the playlist and their corresponding ID number gets stored in an array. Then I index the array looking for the current ID of the song and then when the user hits next it goes to the next song in the array.</p> <p>Here is my code:</p> <pre><code> $(document).ready(function(){ $("#player").prop("volume",".5"); }); var xmlhttp=new XMLHttpRequest(); xmlhttp.open("GET","playlist.xml",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML; var songlist = new Array(); var currentSong; function song_play(song_id){ var player = document.getElementById("player"); player.setAttribute("src", "Music/"+song_id+".mp3"); player.play(); songlist.push(song_id); alert(JSON.stringify(songlist)); currentSong = song_id; var new_id=song_id-1; $("#marquee").empty(); $("#marquee").append("&lt;span style='color:red;'&gt;Now Playing: &lt;/span&gt;"+xmlDoc.getElementsByTagName("artist")[new_id].childNodes[0].nodeValue+"-"); $("#marquee").append(xmlDoc.getElementsByTagName("track")[new_id].childNodes[0].nodeValue); }; function add_song(song_id,new_id){ var artist = xmlDoc.getElementsByTagName("artist")[new_id].childNodes[0].nodeValue; var track = xmlDoc.getElementsByTagName("track")[new_id].childNodes[0].nodeValue; $("#song_list").append("&lt;a id="+song_id+" href='javascript:void(0)' onclick='song_play(this.id);' class='song'&gt;"+artist+"-"+track+"&lt;/a&gt;&lt;br/&gt;"); }; function nextSong(currentSong){ var currentSongIndex = songlist.indexOf(currentSong); var newSong = songlist[currentSongIndex + 1]; song_play(newSong); }; </code></pre> <p>The problem that I am experiencing is that once I hit the next button, it stops playing music all together.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload