forked from benrbryant/JavaScript_APIs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
32 lines (28 loc) · 864 Bytes
/
app.js
File metadata and controls
32 lines (28 loc) · 864 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
32
console.log("Hello World!\n==========\n");
// Exercise 1 Section
console.log("EXERCISE 1:\n==========\n");
const API_KEY = "Nw8eG3P0nmBK2ODO4tuJmnwIK0z7qttG";
let form = document.querySelector("#searchForm");
let searchInput = document.querySelector("#gifTerm");
let img = document.querySelector("img");
form.addEventListener("submit", (event) => {
event.preventDefault();
getGif();
});
function getGif() {
fetch(
`https://api.giphy.com/v1/gifs/translate?api_key=${API_KEY}&s=${searchInput.value}`
)
.then((res) => {
return res.json();
})
.then((body) => {
console.log(body);
img.src = body.data.images.original.url;
img.alt = `${body.data.title} by ${body.data.user.username}`;
img.title = `${body.data.title} by ${body.data.user.username}`;
})
.catch((err) => {
console.log(err);
});
}