-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
41 lines (31 loc) · 1.05 KB
/
script.js
File metadata and controls
41 lines (31 loc) · 1.05 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
async function getCars() {
try {
const brand = document.getElementById("brand").value;
const res = await fetch(`https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMake/${brand}?format=json`);
const data = await res.json();
console.log(data);
const resultDiv = document.getElementById("result");
resultDiv.innerHTML = "";
const cars = data.Results;
if (!cars || cars.length === 0) {
resultDiv.innerHTML = "<p>No cars found</p>";
return;
}
cars.slice(0, 10).forEach(car => {
const modelName = car.Model_Name;
const div = document.createElement("div");
div.innerHTML = `
<h3>${modelName}</h3>
<img
src="https://cdn.imagin.studio/getimage?customer=img&make=${brand}&modelFamily=${modelName}"
width="300"
onerror="this.src='https://via.placeholder.com/300'"
/>
`;
resultDiv.appendChild(div);
});
} catch (error) {
console.log(error);
document.getElementById("result").innerHTML = "<p>Error loading data</p>";
}
}