I have a youtube playlist embedded in an iframe and was hoping to use the playlist index to cycle through an array of titles on-screen (i.e. when the next video starts playing, the next item in the array will show). Is this possible? It only shows the first in the array and doesn't change so what am I doing wrong? Apologies in advance because I'm very new to JS... this might be gibberish but I felt like I was on to something.
var myArray = ["song 1", "song 2"];
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('video', {
events: {
'onReady': onPlayerReady
}
});
}
function onPlayerReady(event) {
console.log(Array.from(document.querySelectorAll('.ytd-playlist-video-list-renderer #video-title')).map((el) => {
return el.textContent.trim()
}).sort().join("\n"))
var Number =
player.getPlaylistIndex();
var print = document.getElementById('print');
print.innerHTML = myArray[Number];
}
<div id="print"></div>
<iframe
id="video"
src="https://www.youtube.com/embed?listType=playlist&list=ID&loop=1&enablejsapi=1&html5=1"
frameborder="0"
allowfullscreen
></iframe>
-
never mind, i figured it out! but will leave here in case someone wants to do something similar. i was using onPlayerReady as the event when i should have used onPlayerStateChange . And the console.log was nonsense that I forgot to delete... ain't that just the way.alaska22– alaska222025年10月09日 04:15:42 +00:00Commented Oct 9, 2025 at 4:15
-
2you should add an answer to your question not a comment if you figured it outLinda Lawton - DaImTo– Linda Lawton - DaImTo2025年10月09日 08:19:19 +00:00Commented Oct 9, 2025 at 8:19
-
1sorry, new to this site! posting now.alaska22– alaska222025年10月10日 00:45:39 +00:00Commented Oct 10, 2025 at 0:45
1 Answer 1
never mind, i figured it out! but will leave here in case someone wants to do something similar. i was using onPlayerReady as the event when i should have used onPlayerStateChange . And the console.log was nonsense that I forgot to delete... ain't that just the way.