1

I have some thumbnails below the main video container. I will like when you click on each thumbnail - the associated video loads and start playing Using the NEW YOUTUBE API IFRAME approach here

Your help or direction will be appreciated

PREVIEW ON JSFIDDLE HERE

PREVIEW LINK UPDATED***

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jerry S.
  • 91
  • 2
  • 10

1 Answers1

3

See this fiddle: http://jsfiddle.net/Y9j7R/5/

Run this code on load:

var a = document.getElementsByTagName("a");            //1
for(var i=0; i<a.length; i++){                         //2
    if(!/#ytplayer/.test(a[i].href)) continue;         //3
    var link = a[i].innerHTML.match(/\/vi\/([^\/]+)/); //4
    if(link) (function(vidId){                         //5
        a[i].onclick = function(){                     //6
            player.loadVideoById(vidId);               //7
        }                                              //8
    })(link[1]);                                       //9
}      

Detailed explanation of the code

  1. Select all <a> (anchor) elements in the document
  2. Loop through these anchors using for. During each iteration, the "current" anchor is referred through a[i].
  3. Check whether the href attribute does not (!) contain "#ytplayer" using the test method of the Regular Expression. If this condition is true (ie: the href attribute does not contain "#ytplayer"), the continue statement terminates the current iteration, and jumps to the next anchor.
  4. The innerHTML property of the current anchor is requested. The match method is used to get the video id. The Regular expression /\/vi\/([^\/]+)/ means: match a substring which equals /vi/<any consecutive non-slash chars>, and group <any consecutive non-slash chars>.
    When such a substring is found, the link variable has a property 1 (one), which holds the value of this group. Otherwise, link equals null.
  5. If the link variable is not null, an anonymous function is created (lines 5-9) and executed (line 9). The first argument of the function will be referenced through vidId (variable).
  6. Assigns a newly created function to the onclick property of the current anchor. Assigning a function to the onclick property will cause the onclick event handler to be defined.
  7. Invokes the loadVideoById method of the player object (as defined by the YouTube javascript API).
  8.  
  9. Invokes the function (created at lines 5-9), passing link[1] as a first parameter.

References

Another interesting answer

Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • HEY ROB...thanks for your reply. Is there any way you can explain this for loop fully.? I understand most of it, but not all. I'm new to Jquery overall. – Jerry S. Oct 11 '11 at 17:27
  • 1
    jQuery? What I've shown is pure JavaScript. What part did you not understand? – Rob W Oct 11 '11 at 17:31
  • THIS: for(var i=0; i – Jerry S. Oct 11 '11 at 18:36
  • @MIS I've added the explanation of my code at the answer, including references. Do you understand it now? – Rob W Oct 11 '11 at 19:57
  • Yep Got it thank You Quick Question. What if I want to Get The title for each video when the videos comes up. Do you have any idea how I go about that. But then i said to myself there must be a way to get it using the DATA API or Player API....still looking into it. Thanks for any help in advance. I was going to do something like this : if (vidId == 'video_id1'){ $("#titlediv").append("VIDEO 1"); } – Jerry S. Oct 11 '11 at 22:52