index.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. window.onload = (event) => {
  2. const searchInput = document.getElementById('search');
  3. searchInput.addEventListener('keyup', (e) => {
  4. console.log(searchInput.value);
  5. const encodedValue = encodeURIComponent(searchInput.value);
  6. fetch('/search?s=' + encodedValue).then(response => {
  7. clearResults();
  8. console.log(response);
  9. response.json().then(content => {
  10. console.log(content);
  11. content.forEach(element => {
  12. addResult(element.title, element.url);
  13. })
  14. });
  15. });
  16. });
  17. };
  18. function clearResults() {
  19. const results = document.getElementById('results');
  20. results.innerHTML = '';
  21. }
  22. function addResult(title, url) {
  23. const par = document.createElement("p");
  24. const link = document.createElement("a");
  25. const linkText = document.createTextNode(title);
  26. link.appendChild(linkText);
  27. link.href = url;
  28. par.appendChild(link);
  29. const results = document.getElementById('results');
  30. results.appendChild(par);
  31. }