-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
67 lines (62 loc) · 1.79 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const elementById = (id) => {
return document.getElementById(id);
};
const handleSearch = () => {
const keyword = elementById("keyword");
const url = `https://theaudiodb.com/api/v1/json/2/search.php?s=${keyword}`;
fetch(url)
.then((res) => res.json())
.then((data) => showArtists(data));
};
const showArtists = (data) => {
const artistContainer = elementById("artists");
data?.artists?.forEach((artist) => {
const div = document.createElement("div");
div.classList.add("artist-card");
div.innerHTML = `<div class="image-container">
<div class="image-container-inner">
<img
src="${artist.strArtistThumb}"
alt=""
/>
</div>
</div>
<div class="info-container">
<h1>${artist.strArtist}</h1>
<p>Country: ${artist.strCountry}</p>
<p>Style: ${artist.strGenre}</p>
</div>
<button class="album-button">
<i class="fa-solid fa-compact-disc"></i>
<p onclick="fetchAlbums('${artist.idArtist}')" class="button-title">Albums</p>
</button>`;
artistContainer.appendChild(div);
});
};
const fetchAlbums = (id) => {
const url = `theaudiodb.com/api/v1/json/2/album.php?i=${id}`;
fetch(url)
.then((res) => res.JSON())
.then((data) => showAlbum(data));
const artistContainer = elementById("artists");
artistContainer.innerHTML = "";
};
const showAlbum = (data) => {
const albumContainer = elementById("albums");
album.forEach((item) => {
const div = document.createElement("div");
div.classList.add("album");
div.innerHTML = `
<div class="album-image-container">
<img
src="${album.strAlbumThumb}"
alt=""
/>
</div>
<div class="album-name">
<h3>${album.strAlbum}</h3>
</div>
`;
albumContainer.appendChild(div);
});
};