function updateVideoTags(sessionLinks, noteLinks) { const sessionIconsContainers =…
- User
function updateVideoTags(sessionLinks, noteLinks) { const sessionIconsContainers = document.querySelectorAll( '.session-icons-container' ); const sessionVideos = document.querySelectorAll('.session-video'); const sessionNotes = document.querySelectorAll('.session-note'); // const videoPlayers = new Array(sessionLinks.length).fill(null); // Array to store YouTube players const notePlayers = new Array(noteLinks.length).fill(null);
// Function to create a new YouTube player with a given video ID function createYouTubePlayer(videoId, index) { if (!videoPlayers[index]) { videoPlayers[index] = new YT.Player(
player${index}, { height: '515', width: '960', videoId: videoId, playerVars: { showinfo: 0, // Hide video title and uploader modestbranding: 1, // Minimal YouTube branding rel: 0, // Hide related videos at the end }, events: { onReady: onPlayerReady, }, }); } }// Function to handle the "onReady" event for the YouTube player function onPlayerReady(event) { // Player is ready, but pause the video immediately event.target.pauseVideo(); }
sessionIconsContainers.forEach((container, index) => { const videoLink = container.querySelector('.video-icon'); videoLink.addEventListener('click', function (e) { e.preventDefault();
// Set the YouTube video ID based on the sessionLinks array const videoId = getYouTubeVideoId(sessionLinks[index]); if (videoId) { createYouTubePlayer(videoId, index); } // Toggle the visibility and play/pause of the video const sessionVideo = sessionVideos[index]; if (sessionVideo.style.display === 'block') { sessionVideo.style.display = 'none'; players[index].pauseVideo(); } else { sessionVideo.style.display = 'block'; players[index].playVideo(); } }); const noteLink = container.querySelector('.notes-icon'); noteLink.addEventListener('click', function (e) { e.preventDefault(); const videoId = getYouTubeVideoId(noteLinks[index]); if (videoId) { createYouTubePlayer(videoId, index); } // Toggle the visibility of the notes const sessionNote = sessionNotes[index]; if (sessionNote.style.display === 'block') { sessionNote.style.display = 'none'; } else { sessionNote.style.display = 'block'; } });}); }
- Assistant
Based on the provided code, it seems like you are missing the code to toggle the visibility and play/pause of the video when the notes icon is clicked.
To fix this, you can modify the event listener for the notes icon click as follows:
noteLink.addEventListener('click', function (e) { e.preventDefault(); const videoId = getYouTubeVideoId(sessionLinks[index]); if (videoId) { createYouTubePlayer(videoId, index); } // Toggle the visibility of the notes and pause the video const sessionNote = sessionNotes[index]; const sessionVideo = sessionVideos[index]; if (sessionNote.style.display === 'block') { sessionNote.style.display = 'none'; videoPlayers[index].pauseVideo(); } else { sessionNote.style.display = 'block'; sessionVideo.style.display = 'none'; videoPlayers[index].pauseVideo(); } });This code first checks if the notes are already displayed. If they are, it hides the notes and pauses the video using the
videoPlayersarray. If the notes are not displayed, it shows the notes and hides the video and also pauses the video using the array.