From 67b6a435baa55932a6bf891dcec83c8b5fbf0a92 Mon Sep 17 00:00:00 2001 From: Jacob Asper Date: Mon, 13 Jun 2022 23:09:59 -0400 Subject: [PATCH 1/4] added function removing default for enter press --- public/js/main.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/public/js/main.js b/public/js/main.js index 0d97e33..aec2958 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -7,6 +7,24 @@ scrollBtn.addEventListener("click", function () { left: 0, }); }); +const input = document.querySelector('input') +//add event listener for when input is focused +input.addEventListener('keydown', (e) => { + if (e.key === 'Enter') { + //stop browser from refreshing + e.preventDefault() + searchOnEnterPress() + //reset search bar to empty + document.querySelector('input').value = '' + } +}); + +function searchOnEnterPress() { + //if enter key is pressed, search + console.log('focused') + getMatches() +} + const btn = document.getElementById('keyword-btn'); btn.addEventListener('click', getMatches); @@ -40,7 +58,7 @@ function renderMatches(matches) { list.innerHTML = ''; // For every match found, render the objects to the DOM in JSON format - if(matches.length > 0) { + if (matches.length > 0) { matches.forEach(match => { const li = document.createElement('li'); From dcfad272738ec8d9a32d9cd7b17fa775e22c76ef Mon Sep 17 00:00:00 2001 From: Jacob Asper Date: Mon, 13 Jun 2022 23:16:28 -0400 Subject: [PATCH 2/4] got rid of unnecessary function --- public/js/main.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/public/js/main.js b/public/js/main.js index aec2958..2e437c2 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -13,18 +13,12 @@ input.addEventListener('keydown', (e) => { if (e.key === 'Enter') { //stop browser from refreshing e.preventDefault() - searchOnEnterPress() + getMatches() //reset search bar to empty document.querySelector('input').value = '' } }); -function searchOnEnterPress() { - //if enter key is pressed, search - console.log('focused') - getMatches() -} - const btn = document.getElementById('keyword-btn'); btn.addEventListener('click', getMatches); From 473c3bf8f4c967960173591c05a05be9576b5209 Mon Sep 17 00:00:00 2001 From: Jacob Asper Date: Mon, 13 Jun 2022 23:21:18 -0400 Subject: [PATCH 3/4] scrolled back to the top on search --- public/js/main.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/public/js/main.js b/public/js/main.js index 2e437c2..9697f5c 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -7,7 +7,9 @@ scrollBtn.addEventListener("click", function () { left: 0, }); }); + const input = document.querySelector('input') +const scrollContainer = document.querySelector('.scroll-container') //add event listener for when input is focused input.addEventListener('keydown', (e) => { if (e.key === 'Enter') { @@ -16,6 +18,8 @@ input.addEventListener('keydown', (e) => { getMatches() //reset search bar to empty document.querySelector('input').value = '' + //scroll back to the top + scrollContainer.scrollTop = 0 } }); From 6b785021f5e0aff3aa099a3a102c59bcca11f2b6 Mon Sep 17 00:00:00 2001 From: Jacob Asper Date: Mon, 13 Jun 2022 23:21:51 -0400 Subject: [PATCH 4/4] moved scroll to the top into getMatches function so it works for button click too --- public/js/main.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/public/js/main.js b/public/js/main.js index 9697f5c..af3f4fa 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -16,10 +16,6 @@ input.addEventListener('keydown', (e) => { //stop browser from refreshing e.preventDefault() getMatches() - //reset search bar to empty - document.querySelector('input').value = '' - //scroll back to the top - scrollContainer.scrollTop = 0 } }); @@ -44,6 +40,10 @@ async function getMatches() { } catch (err) { console.error(err); } + //reset search bar to empty + document.querySelector('input').value = '' + //scroll back to the top + scrollContainer.scrollTop = 0 } /**