Skip to content

Instantly share code, notes, and snippets.

@becsegal
Created March 27, 2018 03:30
Show Gist options
  • Select an option

  • Save becsegal/23274bef2a4a04551f9b0a7479c86bcb to your computer and use it in GitHub Desktop.

Select an option

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

In this exercise, we'll build a page where users can search giphy and see all the results.

Check out the slides for this lesson: // http://goo.gl/WXk5WU

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 Starter Code</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"]}
// 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;
$('h1').text("Results for " + searchTerm);
search(searchTerm);
});
});
function search(searchTerm) {
// STEP 2: build the searchUrl
var apiKey = 'dc6zaTOxFJmzC';
var searchUrl = 'TODO: Build the search url!';
$('#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)
// Show the images from the new results
}
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