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
25 lines (22 loc) · 711 Bytes
/
app.js
File metadata and controls
25 lines (22 loc) · 711 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
'use strict';
const api_key = 'yQ563bcaw5tSmEwXQNzfHBYALvi16iit';
const api_url = 'https://api.giphy.com/v1/gifs/translate?';
function build_url(...pieces) {
pieces.push(`api_key=${api_key}`);
return api_url + pieces.join('&');
}
document.getElementById('search-form').addEventListener('submit', (evt) => {
evt.preventDefault();
let query = document.getElementById('search').value;
let url = build_url(`s=${query}`);
fetch(url, {mode: 'cors'})
.then(response => response.json())
.then(data => {
let img = document.getElementById('gif');
img.src = data.data.images.original.url;
img.alt = data.data.title;
})
.catch((err) => {
console.error(err);
})
})