-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
34 lines (28 loc) · 1.05 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
const searchInput = document.getElementById('search-input');
const resultArtist = document.getElementById('result-artist');
const resultPlaylist = 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) {
resultPlaylist.classList.add('hidden');
const artistName = document.getElementById('artist-name');
const artistImage = document.getElementById('artist-img');
result.forEach(element => {
artistName.innerText = element.name;
artistImage.src = element.urlImg;
});
resultArtist.classList.remove('hidden');
}
document.addEventListener('input', () => {
const searchTerm = searchInput.value.toLowerCase();
if(searchTerm === '') {
resultArtist.classList.add('hidden');
resultPlaylist.classList.remove('hidden');
return;
}
requestAPI(searchTerm);
});