Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make only 5 results to be visible in a search bar at a time

I have an search bar which is working perfectly. But getting an issue. I want only 5 results to be vsible at a time and other results should be seen by sliding from the slider. For example if it contains 10 results. Then is shoould show ony 5 on searching and to see other user can sllide from the slidebar.... here is my code

function filterFunction() {
  let isInputAvail = 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) {
      isInputAvail = true;
      a[i].style.display = "block";
    } else {
      a[i].style.display = "none";
    }
  }
  if (!isInputAvail) {
    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>

Here i want that when i write result then all the matching result is shown but that show a huge list. I want to make it to show ony 5 result and there should be a slidebar at right side from which on sliding other results can be seen. Hope you got my point. Thanks in advance.

like image 613
Coder Avatar asked Sep 14 '25 18:09

Coder


1 Answers

Check out this snippet:

function filterFunction() {
    let isInputAvail = 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) {
            isInputAvail = true;
            a[i].style.display = "block";
        } else {
            a[i].style.display = "none";
        }
    }
    if (!isInputAvail) {
        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 {
    max-height: 215px;
    display: none;
    position: absolute;
    background-color: #f6f6f6;
    min-width: 230px;
    overflow-y: scroll;
    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;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Test</title>
</head>
<body>
    <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>
</body>
</html>

I have limited maximum height of .dropdown-content using max-height property and set vertical overflow to scroll using overflow-y: scroll;. Removed overflow: auto from .dropdown-content.

like image 147
gpl Avatar answered Sep 16 '25 07:09

gpl