File size: 1,817 Bytes
b94886e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html>
  <head>
    <title>API Demo</title>
  </head>
  <body>
    <h1>API Demo</h1>
    <label for="day">Select a day:</label>
    <select id="day">
      <option value="monday">Monday</option>
      <option value="tuesday">Tuesday</option>
      <option value="wednesday">Wednesday</option>
      <option value="thursday">Thursday</option>
      <option value="friday">Friday</option>
      <option value="saturday">Saturday</option>
      <option value="sunday">Sunday</option>
    </select>
    <label for="data">Select data:</label>
    <select id="data" name="data"></select>
    <br><br>
    <input type="button" value="Submit" onclick="fetchData()">

    <!-- <br><br>
    <button onclick="fetchAPI()">Submit</button>
    <br><br> -->
    <div id="result"></div>
    <script>
     function fetchData() {
    const day = document.getElementById("day").value;
    const dataSelect = document.getElementById("data");
    const resultDiv = document.getElementById("result");

    // Clear previous results
    resultDiv.innerHTML = "";

    // Make API request
    fetch(`https://api.example.com/data/${day}`)
        .then(response => response.json())
        .then(data => {
            // Populate data dropdown
            dataSelect.innerHTML = "";
            data.forEach(item => {
                const option = document.createElement("option");
                option.value = item.value;
                option.textContent = item.label;
                dataSelect.appendChild(option);
            });

            // Show fetched data
            resultDiv.innerHTML = `Data for ${day}: ${JSON.stringify(data)}`;
        })
        .catch(error => {
            console.error(error);
            resultDiv.innerHTML = "Error fetching data.";
        });
}

    </script>
  </body>
</html>