Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8495945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:42:59+00:00 2026-06-10T23:42:59+00:00

I inject YouTube iframe to a div in document ready and bind it to

  • 0

I inject YouTube iframe to a div in document ready and bind it to click:

jQuery(document).ready(function($) {
    jQuery('.video-thumb').click(function(){
        ...
        $('#player').html('<iframe width="761" height="421" src="http://www.youtube.com/embed/' + $(this).attr('videoid') + '?rel=0&wmode=transparent" frameborder="0" allowfullscreen></iframe>');
        ...
    }
 }

And I want to do a callback when video ends. I have read about onYouTubePlayerAPIReady, but it has to be put outside document ready.

I have tried to load video player by this construction outside document ready:

var player;
function onYouTubePlayerAPIReady() {
    player = new YT.Player('player', {
      height: '421',
      width: '761',
      videoId: '',
      playerVars: { autoplay: 1, autohide: 1, showinfo: 0 },
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    });
}

but I got some issues:

  • showinfo:0 didn’t work, still got other video thumbnails in the end
  • I don’t know how to change video id (and reinit the video?)
  • more script errors than first method (injecting iframe)

I prefer using first method, but how to create video ends callback? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:43:00+00:00Added an answer on June 10, 2026 at 11:43 pm

    A working example of the below code is also on jsfiddle.net.

    Some notes:

    • Uses the iframe_api, not the javascript_api
    • The YT.Player constructor is minimal because you are building the iframe yourself.
    • The “playerVars” are included as iframe URL parameters.
    • The parameter “enablejsapi=1” is set.

    Example Markup

    <script src="http://www.youtube.com/iframe_api"></script>
    
    <a style="display: block;" class="video-thumb" id="HuGbuEv_3AU" href="#">
        Megadeth: Back In The Day
    </a>  
    <a style="display: block;" class="video-thumb" id="jac80JB04NQ" href="#">
        Judas Priest: Metal Gods
    </a> 
    <a style="display: block;" class="video-thumb" id="_r0n9Dv6XnY" href="#">
        Baltimora: Tarzan Boy
    </a>   
    
    <div id="container"></div>
    <div id="log">--Logging enabled--</div>
    

    The JavaScript

    function log(msg) {
        jQuery('#log').prepend(msg + '<br/>');
    }
    
    function onPlayerStateChange(event) {
        switch(event.data) {
            case YT.PlayerState.ENDED:
                log('Video has ended.');
                break;
            case YT.PlayerState.PLAYING:
                log('Video is playing.');
                break;
            case YT.PlayerState.PAUSED:
                log('Video is paused.');
                break;
            case YT.PlayerState.BUFFERING:
                log('Video is buffering.');
                break;
            case YT.PlayerState.CUED:
                log('Video is cued.');
                break;
            default:
                log('Unrecognized state.');
                break;
        }
    }
    
    jQuery(document).ready(function($) {
        $('.video-thumb').click(function() {
    
            var vidId = $(this).attr('id');
            $('#container').html('<iframe id="player_'+vidId+
                '" width="420" height="315" src="http://www.youtube.com/embed/'+
                vidId+'?enablejsapi=1&autoplay=1&autohide=1&showinfo=0" '+
                'frameborder="0" allowfullscreen></iframe>');
    
            new YT.Player('player_'+vidId, {
                events: {
                    'onStateChange': onPlayerStateChange
                }
            });
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I inject html (iframe containing a video) through javascript is there an event/function
I inject html to div on the page (contains float div) var text =
I need to inject in an iframe window a document object that I instanciated
I want to inject some Json into the Html page returned by the server?
I want to inject dynamically these tags using JavaScript. <meta content='text/html; charset=utf-8' http-equiv='Content-Type'> <meta
How do you inject HTML to a server control? I'm creating a calendar control
Trying to inject my own function (say check) to Float class. When I am
I'm trying to inject some CSS that accompanies some other HTML into a C#
I'd like to inject jQuery into a page using the Google AJAX Libraries API,
Anyone know how to inject an ant build date time stamp into an html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.