-
Notifications
You must be signed in to change notification settings - Fork 0
/
giphy.js
40 lines (34 loc) · 1.17 KB
/
giphy.js
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
33
34
35
36
37
38
39
40
"use stict";
const input = document.querySelector("#searchWord");
const number = document.querySelector("#number");
const btn = document.querySelector("#searchBtn");
btn.addEventListener("click", gifSearch);
number.addEventListener("keyup", enterPressed);
function enterPressed(event) {
if (event.key !== "Enter") return;
else if (number.value === "") {
document.body.innerText = "add number of gifs to display name";
return;
}
gifSearch();
}
function gifSearch() {
const searchedWord = input.value;
const numberOfGif = number.value;
const url = `http://api.giphy.com/v1/gifs/search?q=${searchedWord}&api_key=8Z83DoNxjr9BUrHOrEt2xr4ZYFxodA6Z&limit=${numberOfGif}`;
fetch(url)
.then((response) => response.json())
.then((giphyData) => {
//console.log(giphyData);
renderRetrievedData(giphyData.data);
});
}
function renderRetrievedData(items) {
const searchResult = document.querySelector("#searchResult");
searchResult.innerHTML = "";
items.forEach((element) => {
const searchItem = document.createElement("li");
searchItem.innerHTML = `<img src="${element.images.original.url}">`;
searchResult.appendChild(searchItem);
});
}