· 6 years ago · Nov 27, 2019, 11:16 PM
1$(document).ready(function(){
2 // Handle the search button's click event
3 $("#btnSearch").click(function() {
4
5 // Reset the div element in case a second search is made
6 $("#youtube").html("");
7
8 // Set the search term
9 var searchTerm = "";
10
11 if (!$("#search").val() == "") {
12 searchTerm = $("#search").val();
13
14 } else { // If it is empty, alert the user
15 alert("You must enter a search term!");
16 }
17
18 // Build the custom YouTube URL based on the search term and number of videos
19 // This returns data for only one video as maxResult is set to 1
20 /*** 1. Add your API Key below ***/
21 var url = "https://www.googleapis.com/youtube/v3/search?q=" + searchTerm + "&part=snippet&maxResults=1&key=AIzaSyDO9SgkO_bJfbgxuwN3rwWOgzKSFLhnOnU";
22
23 $.getJSON(url, function(data){
24 //alert(data);
25 var id = data.items[0].id.videoId; /***2. What would be the code here to get the videoID? ***/
26 var title = data.items[0].snippet.title;/***3. What would be the code here to get the title? ***/
27 var link = "https://www.youtube.com/watch?v=" + id;
28 $("#youtube").append("<br> Title:" + title + "<br> Link:" + link.link(link)/***5. Display the data here ***/);
29 });
30
31 });
32});