From 86aef2ce26432246b07fb3550a3de484656385f3 Mon Sep 17 00:00:00 2001 From: Kin Au Date: Thu, 21 Nov 2019 16:39:01 +0000 Subject: [PATCH] add kill all children function Co-authored-by:Roshan --- package.json | 3 ++- public/main.js | 13 +++++++++---- public/request.js | 4 ++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 87737b6..8f0f7a7 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ }, "scripts": { "test": "tape test/test.js | tap-spec", - "cover": "nyc tape test/test.js | tap-spec" + "cover": "nyc tape test/test.js | tap-spec", + "start": "node src/server.js" }, "repository": { "type": "git", diff --git a/public/main.js b/public/main.js index 921c0fd..1d11201 100644 --- a/public/main.js +++ b/public/main.js @@ -1,10 +1,9 @@ const suggestions = document.querySelector(".suggestions"); - -//currentMatches = ["Germany", "Italy", "Paraguay", "Sweden", "UK"]; +let children = document.getElementsByTagName("li"); const createList = () => { + killAllChildren(); currentMatches.forEach( x => { - console.log(currentMatches); let createListItem = document.createElement('li'); let suggestionItem = document.createTextNode(x); createListItem.appendChild(suggestionItem); @@ -12,4 +11,10 @@ const createList = () => { }) } -createList(); \ No newline at end of file +const killAllChildren = () => { + console.log("Killing all children"); + console.log("Here are children: ", children); + while (suggestions.firstChild) { + suggestions.removeChild(suggestions.firstChild); + } +} diff --git a/public/request.js b/public/request.js index 262f8e9..22008cb 100644 --- a/public/request.js +++ b/public/request.js @@ -5,6 +5,9 @@ let currentMatches = []; const api = () => { + if (inputBox.value === "") { + killAllChildren(); + } else { //grab the value in the input box let currentInput = inputBox.value; let xhr = new XMLHttpRequest(); @@ -24,6 +27,7 @@ const api = () => { // Send the search term over to our server xhr.open("GET", updatedSearch, true); xhr.send(); +} }; inputBox.addEventListener("input", api); \ No newline at end of file