In this exercise, you'll add code to the so that you can actually watch the videos on the page.
Either start with your code from the jQuery workshop, or download the starter file.
In the body of your HTML, create a <div> to hold the video player, either below or to the side of the list, and give it an id.
In the script tag that you were working in before, create a thumbnailify() function that takes a single argument.
Move the code that is currently inside your for loop into that thumbnailify() function.
Change the contents of your function so that it uses a single argument, which is a jQuery object that represents an <a>, rather than an array of elements.
Move the for loop so that it occurs after you define thumbnailify()
Call thumbnailify() from inside your for loop.
Make sure your player first works like it did before. Hint: you should only reference an array index from inside of the for loop.
Now, in the thumbnailify() function, add a click listener
to the link. We recommend using a named function for the callback (not an anonymous function). Put a console.log inside the callback to make sure it works.
In the click listener callback, you will be using the "create and store, manipulate, inject" pattern to display an embedded video in the DIV you created.
To find out what the code for an embedded Youtube video looks like, go to Youtube.com and find any embed code
for a video.
You can use youtube.generateEmbedUrl() from the youtube.js library to generate an embed URL.
Use string concatenation, .html(), or other jQuery method of your choice to assemble and insert the embedded video element.
Check your work! Try all your video links. Is there anything you need to change?
As a bonus, try to make the video watcher <div> fade in using jQuery.
Make sure that you use your browser developer tools to make debugging easier
while working on this. Check for errors, and use console.log() to figure out
how far your code makes it, and what the values of your variables are along the way.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Video Watcher</title>
<link rel="stylesheet" type="text/css" href="http://www.teaching-materials.org/common/bootstrap.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="span12">
<h2 class="page-header">Best Videos Ever ❤</h1>
</div>
</div>
<div class="row">
<div class="span3">
<ul>
<li><a href="https://www.youtube.com/watch?v=TddFnTB_7IM">Trip through the 80s</a></li>
<li><a href="https://www.youtube.com/watch?v=epUk3T2Kfno">Otters Holding Hands</a></li>
<li><a href="https://www.youtube.com/watch?v=il2IrgFHfsg">The Ooooh Cat</a></li>
</ul>
</div>
<div class="span9" id="video-watcher">
</div>
</div>
</div>
<script type="text/javascript" src="http://www.teaching-materials.org/common/youtube.js"></script>
<script type="text/javascript" src="http://www.teaching-materials.org/common/jquery.js"></script>
<script>
var thumbnailCallback = function(e) {
e.preventDefault();
var videoEmbed = $('<iframe></iframe>');
videoEmbed.attr('src', youtube.generateEmbedUrl(linkUrl));
videoEmbed.attr('width', 560);
videoEmbed.attr('height', 315);
var videoWatcher = $('#video-watcher');
videoWatcher.hide();
videoWatcher.html(videoEmbed);
videoWatcher.fadeIn();
}
var thumbnailify = function(videoLink) {
var linkUrl = videoLink.attr('href');
var thumbnailUrl = youtube.generateThumbnailUrl(linkUrl);
var thumbnailImg = $('<img>');
thumbnailImg.attr('src', thumbnailUrl);
videoLink.append(thumbnailImg);
videoLink.on('click', thumbnailCallback);
};
var videoLinks = $('a');
for (var i = 0; i < videoLinks.length; i++) {
var videoLink = $(videoLinks[i]);
thumbnailify(videoLink);
}
</script>
</body>
</html>