-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
45 lines (40 loc) · 1.82 KB
/
script.js
File metadata and controls
45 lines (40 loc) · 1.82 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
33
34
35
36
37
38
39
40
41
42
43
44
45
document.getElementById('fetchDriver').addEventListener('click', () => {
const driverInput = document.getElementById("f1Driver");
const driverName = driverInput.value.trim(); // Get the value and trim whitespace
const errorDiv = document.getElementById("error");
const resultsDiv = document.getElementById("results");
// Clear previous error or results
errorDiv.textContent = "";
resultsDiv.innerHTML = "";
if (!driverName) {
errorDiv.textContent = "Please enter a driver's name.";
return;
}
fetch(`https://api.openf1.org/v1/drivers?first_name=${driverName}&session_key=9158`)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
if (data && data.length > 0) {
// Ensure the structure of the response matches the expected format
const driver = data[0]; // Assuming the first result is the correct driver
resultsDiv.innerHTML = `
<h2>Driver Details</h2>
<img src="${driver.headshot_url}" alt="${driver.first_name} ${driver.last_name}" style="width: 200px; height: auto; border-radius: 8px;">
<p><strong>Driver ID:</strong> ${driver.driver_number}</p>
<p><strong>Name:</strong> ${driver.first_name} ${driver.last_name}</p>
<p><strong>Broadcast Name:</strong> ${driver.broadcast_name}</p>
<p><strong>Team:</strong> ${driver.team_name}</p>
<p><strong>Country:</strong> ${driver.country_code}</p>
`;
} else {
errorDiv.textContent = "No driver found with the given name.";
}
})
.catch(error => {
errorDiv.textContent = `An error occurred: ${error.message}`;
});
});