-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.js
37 lines (30 loc) · 1.07 KB
/
search.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
const searchInput = document.getElementById("search-input");
const resultsArtist = document.getElementById("result-artist");
const resultsPlaylist = document.getElementById("result-playlists");
function requestApi(searchTerm) {
const url = `http://localhost:3000/artists?name_like=${searchTerm}`;
fetch(url)
.then((response) => response.json())
.then((result) => displayResults(result));
}
function displayResults(result) {
resultsPlaylist.classList.add("hidden");
const artistName = document.getElementById("artist-name");
const artistImage = document.getElementById("artist-img");
artistName.innerText = "";
artistImage.src = "";
result.forEach((element) => {
artistName.innerText = element.name;
artistImage.src = element.urlImg;
});
resultsArtist.classList.remove("hidden");
}
document.addEventListener("input", function () {
const searchTerm = searchInput.value.toLowerCase();
if (searchTerm === "") {
resultsPlaylist.classList.remove("hidden");
resultsArtist.classList.add("hidden");
return;
}
requestApi(searchTerm);
});