Skip to content

Instantly share code, notes, and snippets.

@jtomchak
Created February 2, 2018 21:36
Show Gist options
  • Save jtomchak/f4ec1fd133f776f2e5c1e3d98a82a7d0 to your computer and use it in GitHub Desktop.
Save jtomchak/f4ec1fd133f776f2e5c1e3d98a82a7d0 to your computer and use it in GitHub Desktop.
Current Meow Click
window.onload = function() {
console.log("OH SNAP, Kittens!!!!");
let searchButton = document.getElementById("searchButton");
let searchInput = document.getElementById("searchInput");
let meowImage = document.getElementById("randomGliphImage");
searchButton.addEventListener("click", fetchGiphy);
function fetchGiphy() {
//Go get the kitten stuff meow!!
let xhr = new XMLHttpRequest();
//open seasme
xhr.open(
"GET",
"https://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=kittens"
);
//what to do when you return?
xhr.onload = function() {
if (xhr.status === 200 && xhr.readyState === 4) {
let payload = JSON.parse(xhr.response);
//we just want the image_url from data!
console.log(payload.data.image_url);
updateMeowImage(payload.data.image_url);
}
};
function updateMeowImage(imageURL) {
//does what with it?
//set the attribute of our image tag to that imageURL string!
//we need access to that image element on the page!!!!
meowImage.src = imageURL;
}
//go get it already
xhr.send();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment