How do I get the YouTube API to load onclick?
<p>I am using the YouTube Iframe as shown below. I want to make my player load on the click of a button instead as soon as the API loads. </p>

<p>I have created a system where a user of my website will be able to choose what videos will be loaded into the playlist, so I cannot hardcode the YouTube video IDs. Is it possible to make an onclick function that fires an event that loads the player? If so, how??</p>

<p>Thanks so much!!! </p>



<pre><code>&lt;script&gt;
  // 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: {    //CAN I REPLACE THE ONREADY EVENT WITH ONE
        'onReady': onPlayerReady, //THAT FIRES ONCLICK? 
        'onStateChange': onPlayerStateChange 
      }
    });
  }

  // 4. The API will call this function when the video player is ready.
  function onPlayerReady(event) {
    event.target.playVideo();
  }
</code></pre>