I’m making a playlist and when you click on one of the videos on the playlist I want it to change to a different video, but I’m not able to get it to work. I want the video to go from episode 1 to episode 2, but how would I go about doing that? I added a click event so when you click on any of the playlist items I want it to do something. I want it to change from episode 1 to episode 2. I tried setting the playlist videos equal to the main video, but that doesn’t change the video. I have my main video on the left handside and my playlist videos on the right hand side.
HTML Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Video Playlist</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="video-and-playlist-container">
<div class="main-video-container">
<video
src="videos/Episode_1.MP4"
loop
controls
poster="image/youtube-overlay.PNG"
class="main-video">
</video>
</div>
<div class="video-list-container">
<div class="playlist-header">
<p class="playlist-header-title">Episode List</p>
</div>
<div class="list-container">
<div class="list active">
<video
src="videos/Episode_1.MP4"
poster="https://i.ytimg.com/vi/_jPLDLngDAs/mqdefault.jpg"
class="list-video">
</video>
<p class="list-title">From Ground Zero to Career Superhero! | Career Transforming Docuseries | Episode 1 | Super Purposes</p>
</div>
<div class="list">
<video
src="videos/Episode_2.MP4"
poster="https://i.ytimg.com/vi/1uBvUObNzVY/hqdefault.jpg?sqp=-oaymwEiCKgBEF5IWvKriqkDFQgBFQAAAAAYASUAAMhCPQCAokN4AQ==&rs=AOn4CLDUvCB8hkoA9x8XKnBduZeLQuXGBQ"
class="list-video">
</video>
<p class="list-title">Career Docuseries | Episode 2: Turning Obstacles into Experience and Using LinkedIn As Your Platform</p>
</div>
<div class="list">
<video
src="videos/Episode_3.mp4"
poster="https://i.ytimg.com/vi/zUVRxR-P69M/hqdefault.jpg?sqp=-oaymwEiCKgBEF5IWvKriqkDFQgBFQAAAAAYASUAAMhCPQCAokN4AQ==&rs=AOn4CLCLE_MEr33No59PuT5wtqy5xQiW6Q"
class="list-video">
</video>
<p class="list-title">Career Docuseries | Episode 3: Tapping Into The Power Of Your LinkedIn Network</p>
</div>
</div>
</div>
<div class="description">
<div class="description-title">Episode 1: Super Purposes</div>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS code:
.container .video-and-playlist-container {
border: 0.5px solid black;
display: flex;
flex-wrap: wrap;
width: 100%;
height: 500px;
flex-direction: row;
align-items: center;
justify-content: center;
}
/* main video container */
.container .video-and-playlist-container .main-video-container {
width: 60%;
height: 450px;
margin-right: 30px;
}
.container .video-and-playlist-container .main-video-container .main-video {
height: 450px;
width: 100%;
}
/* playlist container */
.container .video-and-playlist-container .video-list-container {
width: 30%;
height: 450px;
border: 1px solid black;
overflow: hidden;
}
/*playlist header container - Episode List */
.container .video-and-playlist-container .video-list-container .playlist-header {
display: flex;
align-items: center;
justify-content: center;
height: 40px;
background-color: #3F3D3E;
}
.container .video-and-playlist-container .video-list-container .playlist-header .playlist-header-title {
color: #ffffff;
font-size: 15pt;
font-family: lato, san-serif;
}
/* list container */
.container .video-and-playlist-container .video-list-container .list-container {
height: 100%;
overflow-y: scroll;
}
/* list contains every box for each video and title */
.container .video-and-playlist-container .video-list-container .list-container .list {
display: flex;
align-items: center;
gap: 5px;
cursor: pointer;
border-radius: 5px;
margin-bottom: 10px;
}
.container .video-and-playlist-container .video-list-container .list-container .list:hover {
background-color: #d5dadf;
}
.container .video-and-playlist-container .video-list-container .list-container .list .list-title {
font-family: lato, san-serif;
}
.container .video-and-playlist-container .video-list-container .list-container .list.active {
/*background-color: #f1f3f5;*/
background-color: #d5dadf;
}
.container .video-and-playlist-container .video-list-container .list-container .list .list-video {
width: 200px;
height: 100px;
}
JavaScript Code:
let mainVideo = document.querySelector(".main-video").src;
let listVideos = document.querySelectorAll(".list-video");
const lists = document.querySelectorAll(".list");
for (i = 0; i < lists.length; i++) {
lists[i].addEventListener("click", changeVideo());
}
function changeVideo() {
for (i = 0; i < listVideos.length; i++) {
let srcVideos = listVideos[i].src;
mainVideo = srcVideos;
}
}