Skip to content

Instantly share code, notes, and snippets.

@becsegal
Created March 27, 2018 04:02
Show Gist options
  • Select an option

  • Save becsegal/6d7372853102f55ff6f7fc51de081df2 to your computer and use it in GitHub Desktop.

Select an option

Save becsegal/6d7372853102f55ff6f7fc51de081df2 to your computer and use it in GitHub Desktop.
Exported from Popcode. Click to import: https://popcode.org/?gist=6d7372853102f55ff6f7fc51de081df2

SOLUTION

Step 1:

Get the input value from the input field and assign it to the variable named searchTerm

You'll know it works if when you search for 'dog' it says 'Results for dog' instead of 'Results for undefined'

Step 2:

Build the search url in the search function and assign it to the searchUrl variable

Hints:

  • Check out the gif search api documentation
  • The api key is stored in a variable
  • The search term is a parameter to this function

You'll know it works when you search if the links that appears takes you to a page of search results that you can view with your JSONview chrome extension.

Step 3:

Update the showResults function to display the gifts on the page

First, clear the previous results.

Then add images to the results div.

You'll know it worked because you'll see gifs on the page!

Step 4:

If you finish that and want more, try these extensions:

  • Try the sticker search api
  • Show a different size image than what you have now
  • Link the image back to the source
  • Display the title with the image
<!doctype html>
<html>
<head><title>Giphy Search</title></head>
<body>
<form>
<input id='search'><button>Search</button>
</form>
<h1>Results</h1>
<p><a id='search-link' href='#' target='_blank'></a></p>
<div id='results'>
No results. Search for something!
</div>
</body></html>
{"enabledLibraries":["jquery"],"hiddenUIComponents":["console","editor.css","editor.javascript","editor.html","instructions"]}
// Slides for this lesson:
// http://goo.gl/WXk5WU
// See the instructions for more info!
$(document).ready(function() {
$( "form" ).submit(function( event ) {
// preventDefault stops the normal page refresh that happens when a form is submitted:
event.preventDefault();
// STEP 1: assign the input value to searchTerm
var searchTerm = $('#search').val();
$('h1').text("Results for " + searchTerm);
search(searchTerm);
});
});
function search(searchTerm) {
// STEP 2: build the searchUrl
var apiKey = 'dc6zaTOxFJmzC';
var searchUrl = 'https://api.giphy.com/v1/gifs/search?q=' + searchTerm + '&api_key=' + apiKey;
$('#search-link').attr('href', searchUrl);
$('#search-link').text(searchUrl);
$.ajax({
url: searchUrl,
method: 'GET',
success: function(response) {
showResults(response);
}
});
}
// STEP 3: Update the showResults function to display the gifs on the page
function showResults(results) {
console.log('Showing results');
// Clear the old results display
// (empty the results div)
$('#results').empty();
// Show the images from the new results
results.data.forEach(function(data) {
$('#results').append("<img src=" + data.images.fixed_width.url + ">");
});
}
body {
font-family: sans-serif;
}
input {
font-size: 1.5em;
padding: 5px;
border: 1px solid black;
}
button {
font-size: 1.5em;
padding: 5px 10px;
background: #8ebfdb;
border: 1px solid black;
border-left: 0px;
}
img {
padding: 5px;
vertical-align: middle;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment