I have implemented a filter search feature within two tables on my website.
My goal is to retrieve the search results for a specific drink while keeping the table headings fixed in their positions.
For instance, if I type "Corona" into the search box, I expect to see "Corona" displayed along with the heading for Spirits even if there are no matching items under that category.
I seem to be encountering some issues and can't figure out what went wrong.
* {
box-sizing: border-box;
}
#myInput {
width: 40%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
border-radius:7px;
margin-bottom: 12px;
margin-left:45px;
}
#myUL {
list-style-type: none;
padding: 0;
margin-left:45px;
display: block;
}
#myUL li a {
border: 1px solid #ddd;
border-radius:7px;
margin-top: -1px; /* Prevent double borders */
width:30%;
padding: 8px;
text-decoration: none;
font-size: 18px;
color: black;
background-color:#f6f6f6;
display: block;
}
.t1 {
float:left;
background:yellow;
margin-left:10px;
}
.t2 {
float:left;
background:cyan;
margin-left:50px;
}
li {list-style-type: none;}
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Enter keyword to search" title="Example: Account NT">
<br>
<table class="t1">
<tr>
<th>SPIRITS</th>
</tr>
<ul id="myUL">
<tr><li><td>Armagnac</td></li></tr>
<tr><li><td>Gin</td></li>
<tr><li><td>Vodka</td></li></tr>
</table>
<table class="t2">
<tr>
<th>BEERS</th>
</tr>
<tr><td><li>Budweiser</li></td></tr>
<tr><td><li>Corona</li></td></tr>
<tr><td><li>Heineken</li></td></tr>
</ul>
</table>
<script>
function myFunction() {
var input, filter, ul, li, a, i,;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
ul = document.getElementById("myUL");
li = ul.getElementsByTagName("li");
for (i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}
</script>