Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const form = document.getElementById("search-form");
|
| 2 |
+
const searchBox = document.getElementById("search-box");
|
| 3 |
+
const resultsDiv = document.getElementById("results");
|
| 4 |
+
|
| 5 |
+
form.addEventListener("submit", (event) => {
|
| 6 |
+
event.preventDefault();
|
| 7 |
+
const query = searchBox.value;
|
| 8 |
+
if (query.trim() === "") {
|
| 9 |
+
return;
|
| 10 |
+
}
|
| 11 |
+
search(query);
|
| 12 |
+
});
|
| 13 |
+
|
| 14 |
+
async function search(query) {
|
| 15 |
+
const response = await fetch(`http://example.com/search?q=${query}`);
|
| 16 |
+
const data = await response.json();
|
| 17 |
+
showResults(data.results);
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
function showResults(results) {
|
| 21 |
+
resultsDiv.innerHTML = "";
|
| 22 |
+
for (let result of results) {
|
| 23 |
+
const div = document.createElement("div");
|
| 24 |
+
const title = document.createElement("h2");
|
| 25 |
+
title.textContent = result.title;
|
| 26 |
+
const url = document.createElement("a");
|
| 27 |
+
url.href = result.url;
|
| 28 |
+
url.textContent = result.url;
|
| 29 |
+
div.appendChild(title);
|
| 30 |
+
div.appendChild(url);
|
| 31 |
+
resultsDiv.appendChild(div);
|
| 32 |
+
}
|
| 33 |
+
}
|