-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
23 lines (21 loc) · 834 Bytes
/
script.js
File metadata and controls
23 lines (21 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const memeAPI = "https://api.imgflip.com/get_memes";
const topInput = document.getElementById("top-input");
const bottomInput = document.getElementById("bottom-input");
const img = document.getElementById("img");
const topText = document.getElementById("top-text");
const bottomText = document.getElementById("bottom-text");
const goBtn = document.getElementById("go-btn");
goBtn.addEventListener("click", fetchMemes);
function generateMeme(images) {
const image = images[Math.floor(Math.random() * images.length)];
img.src = image.url;
topText.innerText = topInput.value;
bottomText.innerText = bottomInput.value;
}
function fetchMemes(event) {
event.preventDefault();
fetch(`${memeAPI}`)
.then((response) => response.json())
.then(data => generateMeme(data.data.memes))
.catch(err => console.log(err));
}