forked from benrbryant/JavaScript_APIs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp2.js
More file actions
31 lines (26 loc) · 921 Bytes
/
app2.js
File metadata and controls
31 lines (26 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"use strict";
console.log("Hello Fetch API");
// Program State
const GIPHY_URL = "https://api.giphy.com/v1/gifs/translate";
const GIPHY_KEY = "YizYIssm87tnLV1KsW7XleDxlygvdtQp";
// Select elements
let feedbackEle = document.querySelector("#feedback");
let searchInput = document.querySelector("#searchWord");
let searchBtn = document.querySelector("#submitSearch");
let gifEle = document.querySelector("#imageContainer > img");
// Event Handlers
searchBtn.addEventListener("click", (event) => {
fetch(`${GIPHY_URL}?api_key=${GIPHY_KEY}&s=${searchInput.value}`)
.then((res) => res.json())
.then((body) => {
// Show the gif on the dom
gifEle.src = body.data.images.original.url;
feedbackEle.textContent = "";
})
.catch((err) => {
console.error(err);
// Show the error message on the dom
feedbackEle.textContent = err.message;
});
searchInput.value = "";
});