title: Play Youtube video on click
body: <p>I'm using Youtube API, and have several Divs as thumbnails. When one thumbnail (div) is clicked, the corresponding Youtube video should play. I'm using an iframe element for the video, and the webpage is made up of only HTML, CSS and JavaScript. If I don't use a button (thumbnails/div with onclick) but rather have a blank page with the Youtube API JavaScript code, it'll work. However, when I try to wrap everything inside a function call (from the div's onclick) nothing happens, but I get this error. </p>
  
  <blockquote>
    <p>"Refused to execute script from '<a href="https://www.youtube.com/embed/HVldRjNb4BE" rel="noreferrer">https://www.youtube.com/embed/HVldRjNb4BE</a>' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled."</p>
  </blockquote>
  
  <p>Here is my code:</p>
  
  <h1>HTML:</h1>
  
  <pre><code>&lt;div id="player"&gt;&lt;/div&gt;
  </code></pre>
  
  <h1>JavaScript:</h1>
  
  <pre><code>// 2. This code loads the IFrame Player API code asynchronously.
    var tag = document.createElement('script');
  
    tag.src = "https://www.youtube.com/iframe_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  
    // 3. This function creates an &lt;iframe&gt; (and YouTube player)
    //    after the API code downloads.
    var player;
    function onYouTubeIframeAPIReady() {
      player = new YT.Player('player', {
        height: '390',
        width: '640',
        videoId: 'M7lc1UVf-VE',
        events: {
          'onReady': onPlayerReady,
          'onStateChange': onPlayerStateChange
        }
      });
    }
  
    // 4. The API will call this function when the video player is ready.
    function onPlayerReady(event) {
      event.target.playVideo();
    }
  
    // 5. The API calls this function when the player's state changes.
    //    The function indicates that when playing a video (state=1),
    //    the player should play for six seconds and then stop.
    var done = false;
    function onPlayerStateChange(event) {
      if (event.data == YT.PlayerState.PLAYING &amp;&amp; !done) {
        //setTimeout(stopVideo, 6000);
        done = true;
      }
    }
    function stopVideo() {
      player.stopVideo();
    }
  </code></pre>
  
  <p>This code is just copied from <a href="https://developers.google.com/youtube/iframe_api_reference" rel="noreferrer">https://developers.google.com/youtube/iframe_api_reference</a>. </p>
  
  <p>When this is all I have in the document, a player is there when the page loads, and the video is ready to play. 
  So to clarify my question, how can I make it so that when a user clicks one of the buttons (div) the Youtube player displays and play the video?</p>