Skip to content
Advertisement

HTML + JavaScript custom player for multiple videos on a website page

EDIT: I have found the solution, posted and marked as the best answer below.

I’m coding a portfolio website for myself using HTML, CSS and JS and I need to add multiple videos on a lot of pages.

I followed some tutorials to learn how to customize the video player, but it only works for one specific video on the page. If I were to add more videos, I’d need to have one .js custom player file for each video and manually select them on the website.

How can I apply this single .js custom video player to all of my videos using purely javascript?

I have found similar topics about the subject here, but all of them uses jQuery and I’m struggling to make it work with javascript.

My HTML for the video player:

JavaScript

My JavaScript for the video player:

JavaScript

Advertisement

Answer

I HAVE FOUND THE SOLUTION

For anyone in the future seeking for the same solution, I was able to make it work by first selecting every videoplayer div:

JavaScript

Then using the .forEach() function so that the code will generate a button for every individual videoplayer:

JavaScript

You can reference “container” for the individual videoplayer or “document” for every videoplayer at once. It just works like magic!

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement