Change the id of a Youtube video in HTML

4

I'm trying to change the id of a video in my html, which is in another javascript file, where the youtube API is created. I also read the ids of a JSON file.

HTML

<div class="video-container">
   <div id="player">
      <script>
         function routes(arr) {
            var out = "";
            var a = 3;
            for(i = 2; i<a; i++) {
               out = arr[i].videoID;
               player.loadVideoById("out");
            }           
      </script>
   </div>
</div>

Javascript

 var tag = document.createElement('script');
 tag.src = "https://www.youtube.com/iframe_api";
 var firstScriptTag = document.getElementsByTagName('script')[0];
 firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

 var player;
 function onYouTubeIframeAPIReady() {
   player = new YT.Player('player', {
     playerVars: { 'autoplay': 0, 'controls': 2 },
     height: '390',
     width: '640',
     //videoId: 'xck9LoSvDvc',
     events: {
       'onReady': onPlayerReady,
     }
   });
 }

But I get the following error:

Uncaught SyntaxError: Unexpected end of input

on the line;

player.loadVideoById("out");

could you help me please!

thank you very much!

    
asked by topi 07.05.2016 в 14:37
source

2 answers

3

The problem is that the loadVideoById() method should receive the id of the videp and you are adding a string "out" which is incorrect, it should be:

 out = arr[i].videoID;
 player.loadVideoById(out);

there is also a} missing to close your function.

<script>
         function routes(arr) {
            var out = "";
            var a = 3;
            for(i = 2; i<a; i++) {
               out = arr[i].videoID;
               player.loadVideoById(out);
            }    
         }       
      </script>
    
answered by 08.05.2016 в 08:50
0

The last key } in the script is missing

  <script>
     function routes(arr) {
        var out = "";
        var a = 3;
        for(i = 2; i<a; i++) {
           out = arr[i].videoID;
           player.loadVideoById("out");
        }           
     } // falta
  </script>
    
answered by 07.05.2016 в 19:22