-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
53 lines (46 loc) · 1.47 KB
/
script.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
41
42
43
44
45
46
47
48
49
50
51
52
53
// fetching data from api and checking errors if any
fetch('https://api.imgflip.com/get_memes')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
// rendering the memes
.then(result => {
const memes = result.data.memes;
renderMemes(memes);
})
// catching errors if any
.catch(error => console.error("Error occurred while fetching data", error));
// Function to render the memes
function renderMemes(memes) {
const cardContainer = document.querySelector(".cardContainer");
// Looping through each meme and create a card for it
memes.forEach(meme => {
const { name, url } = meme;
// creating a card for each meme
const card = createCard(name, url);
cardContainer.appendChild(card);
});
}
// Function to create a card for a meme
function createCard(name, url) {
// Create a div element for the card
const card = document.createElement("div");
card.classList.add("col-lg-4", "col-md-6", "mb-4");
// HTML content for the card element
const cardContent = `
<div class="card">
<img src="${url}" class="card-img-top" alt="${name}">
<div class="card-body">
<h5 class="card-title">${name}</h5>
<a href="${url}" class="btn btn-primary" target="_blank">View Full Image</a>
</div>
</div>
`;
// Set the HTML content to the card element
card.innerHTML = cardContent;
// Return the card element
return card;
}