-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
35 lines (31 loc) · 1.06 KB
/
search.js
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
const searchForm = document.getElementById('search-form');
const searchInput = document.getElementById('search-input');
const searchResults = document.getElementById('search-results');
searchForm.addEventListener('submit', function(event) {
event.preventDefault();
const query = searchInput.value;
fetch(`https://free-car-api.com/search?query=${query}`)
.then(response => response.json())
.then(data => {
// Handle the API response and display results
displaySearchResults(data);
})
.catch(error => {
console.error('Error:', error);
});
});
function displaySearchResults(results) {
// Clear previous results
searchResults.innerHTML = '';
// Display each search result
results.forEach(result => {
const carElement = document.createElement('div');
carElement.innerHTML = `
<h3>${result.make} ${result.model}</h3>
<p>Year: ${result.year}</p>
<p>Price: ${result.price}</p>
<p>Location: ${result.location}</p>
`;
searchResults.appendChild(carElement);
});
}