I have a search bar that is currently working perfectly, but I am facing an issue. I want only 5 results to be visible at a time, and the remaining results should be seen by sliding from the slider. For example, if there are 10 results, then only 5 should show on searching and users can slide from the slidebar to see the rest. Here is my code:
function filterFunction() {
let isInputAvailable = false;
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toLowerCase();
if (filter.length > 0) {
document.getElementById("myDropdown").classList.add("show");
} else {
document.getElementById("myDropdown").classList.remove("show");
}
div = document.getElementById("myDropdown");
a = div.getElementsByTagName("a");
for (i = 0; i < a.length; i++) {
txtValue = a[i].innerText;
if (txtValue.toLowerCase().indexOf(filter) > -1) {
isInputAvailable = true;
a[i].style.display = "block";
} else {
a[i].style.display = "none";
}
}
if (!isInputAvailable) {
document.getElementById("noMatches").classList.add('show');
} else {
document.getElementById("noMatches").classList.remove('show');
}
}
.div {
display: none;
}
.dropbtn {
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
border-radius: 25px;
}
#myInput {
box-sizing: border-box;
background-position: 14px 12px;
background-repeat: no-repeat;
font-size: 16px;
padding: 14px 20px 12px 45px;
border: 5px solid #ddd;
border-radius: 25px;
}
#myInput:focus {
outline: 4px solid #f2f2f2;
border-color: #171313;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f6f6f6;
min-width: 230px;
overflow: auto;
border: none;
z-index: 1;
border-radius: 25px;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {
background-color: #ddd;
}
.show {
display: block;
}
<div class="dropdown">
<input type="text" class="dropbtn" placeholder="Search Here..." id="myInput" onInput="filterFunction()">
<div id="myDropdown" class="dropdown-content">
<a href="#">Result 1</a>
<a href="#">Result 2</a>
<a href="#">Result 3</a>
<a href="#">Result 4</a>
<a href="#">Result 5</a>
<a href="#">Result 6</a>
<a href="#">Result 7</a>
<a href="#">Result 8</a>
<a href="#">Result 9</a>
<a href="#">Result 10</a>
<a href="#">Result 11</a>
<a href="#">Result 12</a>
<a href="#">Result 13</a>
<a href="#">Result 14</a>
<a href="#">Result 15</a>
</div>
<div id="noMatches" class="dropdown-content">
<a href="#tools">No Matches</a>
</div>
</div>
I want the matching results to be shown when searched, but with a limitation of showing only 5 at a time. There should also be a slider on the right side from which users can slide to view other results. I hope you understand what I'm looking for. Thank you in advance.