Skip to content

Instantly share code, notes, and snippets.

@NickFoden
Created March 18, 2017 18:19
Show Gist options
  • Save NickFoden/13fa98b12205c7b8b9a400651d1aefcb to your computer and use it in GitHub Desktop.
Save NickFoden/13fa98b12205c7b8b9a400651d1aefcb to your computer and use it in GitHub Desktop.
Challenge Tube
var YOUTUBE_BASE_URL = 'https://www.googleapis.com/youtube/v3/search /*add your api key*/
function getDataFromApi(searchTerm, callback) {
var settings = {
url: YOUTUBE_BASE_URL,
data: {
part: 'snippet',
q: searchTerm,
},
dataType: 'json',
type: 'GET',
success: callback
};
$.ajax(settings);
}
function displayYoutubeSearchData(data) {
var resultElement = '';
if (data.items) {
data.items.forEach(function(item) {
resultElement += '<p>' + item.snippet.title + '</p>';
});
}
else {
resultElement += '<p>No results</p>';
}
$('.js-search-results').html(resultElement);
}
function watchSubmit() {
$('.js-search-form').submit(function(e) {
e.preventDefault();
var query = $(this).find('.js-query').val();
getDataFromApi(query, displayYoutubeSearchData);
});
}
$(function(){watchSubmit();});
<!DOCTYPE html>
<html>
<head>
<title>Search YouTube</title>
</head>
<body>
<h3>Search Youtube from this page</h3>
<form action="#" class="js-search-form">
<label for="query"></label>
<input type="text" class="js-query">
<button type="submit">Search</button>
</form>
<div class="js-search-results">
</div>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script type="text/javascript" src="app.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment