GentleClash

Frontend

Jan 21st, 2024
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.25 KB | Source Code | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.   <title>Dropdown Search</title>
  7.   <style>
  8.     #myInput {
  9.       padding: 10px;
  10.     }
  11.     .dropdown-content {
  12.       display: none;
  13.       position: absolute;
  14.       background-color: #f9f9f9;
  15.       min-width: 160px;
  16.       box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  17.       z-index: 1;
  18.     }
  19.     .dropdown-content li {
  20.       padding: 12px 16px;
  21.       text-decoration: none;
  22.       display: block;
  23.       cursor: pointer;
  24.     }
  25.   </style>
  26. </head>
  27. <body>
  28. <div class="dropdown">
  29.   <input type="text" id="myInput" oninput="filterFunction()" placeholder="Type to search...">
  30.   <ul id="myDropdown" class="dropdown-content" onclick="selectOption()"></ul>
  31. </div>
  32. <script>
  33. // Sample array of options
  34. var optionsArray = ["Apple", "Orange", "Banana", /* Add your 170 options here */];
  35. // Function to create list items from the array
  36. function populateDropdown() {
  37.   var dropdown = document.getElementById("myDropdown");
  38.   dropdown.innerHTML = ""; // Clear previous content
  39.   for (var i = 0; i < optionsArray.length; i++) {
  40.    var listItem = document.createElement("li");
  41.    listItem.textContent = optionsArray[i];
  42.    dropdown.appendChild(listItem);
  43.  }
  44. }
  45. function filterFunction() {
  46.  var input, filter, dropdown, options, i;
  47.  input = document.getElementById("myInput");
  48.  filter = input.value.toUpperCase();
  49.  dropdown = document.getElementById("myDropdown");
  50.  options = dropdown.getElementsByTagName("li");
  51.  if (filter === "") {
  52.    dropdown.style.display = "none";
  53.    return;
  54.  }
  55.  for (i = 0; i < options.length; i++) {
  56.    var optionText = options[i].textContent.toUpperCase();
  57.    if (optionText.startsWith(filter)) {
  58.      options[i].style.display = "";
  59.    } else {
  60.      options[i].style.display = "none";
  61.    }
  62.  }
  63.  dropdown.style.display = "block";
  64. }
  65. function selectOption() {
  66.  var input = document.getElementById("myInput");
  67.  var dropdown = document.getElementById("myDropdown");
  68.  if (event.target.tagName === 'LI') {
  69.    input.value = event.target.textContent;
  70.    dropdown.style.display = "none";
  71.  }
  72. }
  73. // Initial population of the dropdown
  74. populateDropdown();
  75. </script>
  76. </body>
  77. </html>
Advertisement
Add Comment
Please, Sign In to add comment