Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Dropdown Search</title>
- <style>
- #myInput {
- padding: 10px;
- }
- .dropdown-content {
- display: none;
- position: absolute;
- background-color: #f9f9f9;
- min-width: 160px;
- box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
- z-index: 1;
- }
- .dropdown-content li {
- padding: 12px 16px;
- text-decoration: none;
- display: block;
- cursor: pointer;
- }
- </style>
- </head>
- <body>
- <div class="dropdown">
- <input type="text" id="myInput" oninput="filterFunction()" placeholder="Type to search...">
- <ul id="myDropdown" class="dropdown-content" onclick="selectOption()"></ul>
- </div>
- <script>
- // Sample array of options
- var optionsArray = ["Apple", "Orange", "Banana", /* Add your 170 options here */];
- // Function to create list items from the array
- function populateDropdown() {
- var dropdown = document.getElementById("myDropdown");
- dropdown.innerHTML = ""; // Clear previous content
- for (var i = 0; i < optionsArray.length; i++) {
- var listItem = document.createElement("li");
- listItem.textContent = optionsArray[i];
- dropdown.appendChild(listItem);
- }
- }
- function filterFunction() {
- var input, filter, dropdown, options, i;
- input = document.getElementById("myInput");
- filter = input.value.toUpperCase();
- dropdown = document.getElementById("myDropdown");
- options = dropdown.getElementsByTagName("li");
- if (filter === "") {
- dropdown.style.display = "none";
- return;
- }
- for (i = 0; i < options.length; i++) {
- var optionText = options[i].textContent.toUpperCase();
- if (optionText.startsWith(filter)) {
- options[i].style.display = "";
- } else {
- options[i].style.display = "none";
- }
- }
- dropdown.style.display = "block";
- }
- function selectOption() {
- var input = document.getElementById("myInput");
- var dropdown = document.getElementById("myDropdown");
- if (event.target.tagName === 'LI') {
- input.value = event.target.textContent;
- dropdown.style.display = "none";
- }
- }
- // Initial population of the dropdown
- populateDropdown();
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment