I am making my first search bar/engine. I want to make the search bar respond to user inputs so that it could send them to a specific website. The issue I have is that my search button is not sending the user to a webpage when the user inputs certain keywords from texts inside <li><a> </a></li>
into the <input></input>
and presses the search button. How do I fix this?
Edit:
Website: http://techteach.us/Web2020/ZWeiJian/WCP/Labs/Lab_01/Lab_1.html
//Search engine functionality
var sForm = document.getElementById("srchFrm");
document.addEventListener("click", function(event) {
var clickedInside = sForm.contains(event.target);
if (clickedInside) {
//Displaying the search suggestions
document.getElementById("srchRslts").style.display = "block";
} else {
//Hiding the search suggestions
document.getElementById("srchRslts").style.display = "none";
}
});
//Credit to W3Schools
function searchingResults() {
// Declaring variables
let input, filter, ul, li, a, i, txtValue;
input = document.getElementById('srchBar');
filter = input.value;
ul = document.getElementById("srchRslts");
li = ul.getElementsByTagName('li');
// Loop through all list items, and hide those who don't match the search query
for (i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
txtValue = a.textContent || a.innerText;
if (txtValue.indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
//Credit to Textfixer.com and https://stackoverflow.com/questions/155188/trigger-a-button-click-with-javascript-on-the-enter-key-in-a-text-box for the search button code.
//Get the Search Button
var submitButton = document.getElementById("sbmtBtn");
//Add event listener to the submit button
input.addEventListener("keyup", function(e) {
e.preventDefault();
//Press enter to activate the search engine
if (event.keyCode === 13) {
submitButton.click();
}
});
function cSbmtBtn() {
a = li[i].getElementsByTagName("a")[0];
txtValue = a.textContent || a.innerText;
if (filter == txtValue) {
submitButton.value = txtValue;
}
}
}
<!--Basic Search Bar.-->
<form id="srchFrm">
<input id="srchBar" type="text" onKeyUp="searchingResults();" placeholder="Search...">
<button type="submit" id="sbmtBtn" value="" onClick="cSbmtBtn"><i class="fa fa-search"></i></button>
<ul id="srchRslts">
<li><a href="Lab_1.html">Home</a></li>
<li><a href="Lb1Labs.html">Labs</a></li>
<li><a href="Lb1Projects.html">Projects</a></li>
<li><a href="https://qwfepfp.blogspot.com/2019/09/blog-intromy-bio-2.html">Blogger</a></li>
<li><a href="http://techteach.us/index.html">Techteach.us</a></li>
<li><a href="http://techteach.us/Web2020/">Parent Directory</a></li>
<li><a href="http://techteach.us/index.html">Mrs. Ramirez's Site</a></li>
<li><a href="https://www.infotechhs.net/">Information Technology High School Website</a></li>
<li><a href="Lab_1Redirectory.html">Lab 1</a></li>
<li><a href="../Lab_02/Lab_2.html">Lab 2</a></li>
<li><a href="../Lab_03/Lab_3.html">Lab 3</a></li>
<li><a href="../Lab_04/Lab_4.html">Lab 4</a></li>
<li><a href="../Lab_05/Lab_5.html">Lab 5</a></li>
<li><a href="../Lab_06/Lab_6.html">Lab 6</a></li>
<li><a href="Lb1ErrorPage.html">Lab 7</a></li>
<li><a href="Lb1ErrorPage.html">Lab 8</a></li>
<li><a href="Lb1ErrorPage.html">Lab 9</a></li>
<li><a href="Lb1ErrorPage.html">Lab 10</a></li>
<li><a href="Lb1ErrorPage.html">Lab 11</a></li>
<li><a href="Lb1ErrorPage.html">Lab 12</a></li>
<li><a href="Lb1ErrorPage.html">Lab 13</a></li>
<li><a href="Lb1ErrorPage.html">Lab 14</a></li>
<li><a href="Lb1ErrorPage.html">Lab 15</a></li>
<li><a href="Lb1ErrorPage.html">Lab 16</a></li>
<li><a href="Lb1ErrorPage.html">Lab 17</a></li>
<li><a href="Lb1ErrorPage.html">Lab 18</a></li>
<li><a href="Lb1ErrorPage.html">Lab 19</a></li>
<li><a href="Lb1ErrorPage.html">Lab 20</a></li>
<li><a href="../../Projects/Pr1/index.html">Project 1</a></li>
<li><a href="../../Projects/ECONO/ECONO.html">Project 2</a></li>
<li><a href="Lb1ErrorPage.html">Project 3</a></li>
<li><a href="Lb1ErrorPage.html">Project 4</a></li>
<li><a href="Lb1About.html">About</a></li>
<li><a href="Lb1PD.html">Privacy & Disclaimer</a></li>
<li><a href="Lb1TS.html">Terms of Service</a></li>
<li><a href="Lb1Citations.html">Citation</a></li>
</ul>
</form>