-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
32 lines (29 loc) · 1.04 KB
/
script.js
File metadata and controls
32 lines (29 loc) · 1.04 KB
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
document.addEventListener('DOMContentLoaded', function() {
const searchButton = document.querySelector('.btn');
const activityContainer = document.querySelector('.activity');
searchButton.addEventListener('click', getRandomActivity);
function getRandomActivity() {
const apiUrl = 'https://www.boredapi.com/api/activity';
// Make API request
fetch(apiUrl)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
displayActivity(data.activity);
})
.catch(error => {
console.error('Error fetching activity:', error);
});
}
function displayActivity(activity) {
let ul = document.querySelector(".ul")
ul.innerHTML = ""; // Clear the ul
let li = document.createElement("li");
li.innerText = activity;
ul.appendChild(li);
}
});