File size: 1,259 Bytes
a977abc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const form = document.querySelector('form');
const input = document.querySelector('input[type="text"]');
const searchButton = document.querySelector('input[type="submit"]');

const resultsDiv = document.querySelector('.results');

form.addEventListener('submit', handleSubmit);
searchButton.addEventListener('click', handleSubmit);

function handleSubmit(event) {
 event.preventDefault();
 const inputValue = input.value;
 if (inputValue.length > 0) {
    const request = new XMLHttpRequest();
    request.open('GET', `//api.dataseer.com/v1/music?keywords=${inputValue}`, true);
    request.onload = function () {
      if (request.status === 200) {
        const data = JSON.parse(request.responseText);
        if (data.results) {
          renderResults(data.results);
        }
      } else {
        alert('Error getting results');
      }
    };
    request.send();
 }
}

function renderResults(results) {
 resultsDiv.innerHTML = '';
 results.forEach((result) => {
    const artist = document.createElement('div');
    artist.className = 'result';
    const title = document.createElement('h3');
    const text = document.createTextNode(result.name);
    title.appendChild(text);
    artist.appendChild(title);
    resultsDiv.appendChild(artist);
 });
}