Issue muting a youtube video

Hi fellow coders! I am finishing a project about space. However Im having troubles muting a youtube video I embed.

So I basically used this code to mute a video. (And Its working wonderfuly) The issue comes now! I want to mute another video on my website but I really dont know how to do it. Copying the same code but with another video ID just doesn’t work. Sorry my Javascript knowledge is almost=0.

Thank you very much to everyone reading this post :smiley:

I leave my codepen project here just in case you need more information about the code.

Hi there,

It helps a lot if you post your actual code rather than a screenshot. I’d like to copy-and-paste your work rather than having to type stuff myself. Programmers are quite lazy like this.

When you do post code, you keep it between three backticks ( ` ) in order to format it correctly. See this post for details.

So, I’ve never used the YouTube embed API before, but it seems that you instantiate a new player with the video container’s ID passed to the constructor and the video ID in the options object. If you want more videos with the exact same options, you’ll just need to change those IDs.

In your HTML

  <div id="firstVideo"></div>
  <div id="secondVideo"></div>

In your JavaScript

var firstVideo = new YT.player('firstVideo', {
    videoId: 'firstVideoId',
    //... the other stuff that I don't want to type

var secondVideo = new YT.player('secondVideo', {
    videoId: 'secondVideoId'
    //... the same stuff that I still don't want to type

This is a great use case for functions!

function createMutedYouTubeVideo(divId, videoId) {
    return new YT.player(divId, {
        videoId: videoId
        //... the same stuff that I still don't want to type

var firstVideo = createMutedYouTubeVideo('firstVideo', 'firstVideoId');
var secondVideo = createMutedYouTubeVideo('secondVideo', 'secondVideoId');