Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  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=###########################";
  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));
  29. });
  30.  
  31. });
  32. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement