Created
November 13, 2021 22:48
-
-
Save cagataycali/b85b2733b1c568f4fed3e22176470911 to your computer and use it in GitHub Desktop.
[JavaScript] Auto complete
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8" /> | |
| <meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
| <title>Auto complete</title> | |
| <style> | |
| .auto-complete { | |
| width: 300px; | |
| display: flex; | |
| flex-direction: column; | |
| } | |
| .auto-complete-items { | |
| display: none; | |
| } | |
| .form { | |
| display: flex; | |
| align-items: flex-start; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <h1>Auto complete</h1> | |
| <!-- Create a form --> | |
| <form autocomplete="off" onsubmit="return false" class="form"> | |
| <div class="auto-complete"> | |
| <input type="text" name="search" id="search" placeholder="Search" /> | |
| <div id="auto-complete-items" aria-hidden="true"></div> | |
| </div> | |
| <input type="submit" value="Search" /> | |
| </form> | |
| <script> | |
| const countries = [ | |
| "Afghanistan", | |
| "Albania", | |
| "Algeria", | |
| "Andorra", | |
| "Angola", | |
| "Anguilla", | |
| "Antigua & Barbuda", | |
| "Argentina", | |
| "Armenia", | |
| "Aruba", | |
| "Australia", | |
| "Austria", | |
| "Azerbaijan", | |
| "Bahamas", | |
| "Bahrain", | |
| "Bangladesh", | |
| "Barbados", | |
| "Belarus", | |
| "Belgium", | |
| "Belize", | |
| "Benin", | |
| "Bermuda", | |
| "Bhutan", | |
| "Bolivia", | |
| "Bosnia & Herzegovina", | |
| "Botswana", | |
| "Brazil", | |
| "British Virgin Islands", | |
| "Brunei", | |
| "Bulgaria", | |
| "Burkina Faso", | |
| "Burundi", | |
| "Cambodia", | |
| "Cameroon", | |
| "Canada", | |
| "Cape Verde", | |
| "Cayman Islands", | |
| "Central Arfrican Republic", | |
| "Chad", | |
| "Chile", | |
| "China", | |
| "Colombia", | |
| "Congo", | |
| "Cook Islands", | |
| "Costa Rica", | |
| "Cote D Ivoire", | |
| "Croatia", | |
| "Cuba", | |
| "Curacao", | |
| "Cyprus", | |
| "Czech Republic", | |
| "Denmark", | |
| "Djibouti", | |
| "Dominica", | |
| "Dominican Republic", | |
| "Ecuador", | |
| "Egypt", | |
| "El Salvador", | |
| "Equatorial Guinea", | |
| "Eritrea", | |
| "Estonia", | |
| "Ethiopia", | |
| "Falkland Islands", | |
| "Faroe Islands", | |
| "Fiji", | |
| "Finland", | |
| "France", | |
| "French Polynesia", | |
| "French West Indies", | |
| "Gabon", | |
| "Gambia", | |
| "Georgia", | |
| "Germany", | |
| "Ghana", | |
| "Gibraltar", | |
| "Greece", | |
| "Greenland", | |
| "Grenada", | |
| "Guam", | |
| "Guatemala", | |
| "Guernsey", | |
| "Guinea", | |
| "Guinea Bissau", | |
| "Guyana", | |
| "Haiti", | |
| "Honduras", | |
| "Hong Kong", | |
| "Hungary", | |
| "Iceland", | |
| "India", | |
| "Indonesia", | |
| "Iran", | |
| "Iraq", | |
| "Ireland", | |
| "Isle of Man", | |
| "Israel", | |
| "Italy", | |
| "Jamaica", | |
| "Japan", | |
| "Jersey", | |
| "Jordan", | |
| "Kazakhstan", | |
| "Kenya", | |
| "Kiribati", | |
| "Kosovo", | |
| "Kuwait", | |
| "Kyrgyzstan", | |
| "Laos", | |
| "Latvia", | |
| "Lebanon", | |
| "Lesotho", | |
| "Liberia", | |
| "Libya", | |
| "Liechtenstein", | |
| "Lithuania", | |
| "Luxembourg", | |
| "Macau", | |
| "Macedonia", | |
| "Madagascar", | |
| "Malawi", | |
| "Malaysia", | |
| "Maldives", | |
| "Mali", | |
| "Malta", | |
| "Marshall Islands", | |
| "Mauritania", | |
| "Mauritius", | |
| "Mexico", | |
| "Micronesia", | |
| "Moldova", | |
| "Monaco", | |
| "Mongolia", | |
| "Montenegro", | |
| "Montserrat", | |
| "Morocco", | |
| "Mozambique", | |
| "Myanmar", | |
| "Namibia", | |
| "Nauro", | |
| "Nepal", | |
| "Netherlands", | |
| "Netherlands Antilles", | |
| "New Caledonia", | |
| "New Zealand", | |
| "Nicaragua", | |
| "Niger", | |
| "Nigeria", | |
| "North Korea", | |
| "Norway", | |
| "Oman", | |
| "Pakistan", | |
| "Palau", | |
| "Palestine", | |
| "Panama", | |
| "Papua New Guinea", | |
| "Paraguay", | |
| "Peru", | |
| "Philippines", | |
| "Poland", | |
| "Portugal", | |
| "Puerto Rico", | |
| "Qatar", | |
| "Reunion", | |
| "Romania", | |
| "Russia", | |
| "Rwanda", | |
| "Saint Pierre & Miquelon", | |
| "Samoa", | |
| "San Marino", | |
| "Sao Tome and Principe", | |
| "Saudi Arabia", | |
| "Senegal", | |
| "Serbia", | |
| "Seychelles", | |
| "Sierra Leone", | |
| "Singapore", | |
| "Slovakia", | |
| "Slovenia", | |
| "Solomon Islands", | |
| "Somalia", | |
| "South Africa", | |
| "South Korea", | |
| "South Sudan", | |
| "Spain", | |
| "Sri Lanka", | |
| "St Kitts & Nevis", | |
| "St Lucia", | |
| "St Vincent", | |
| "Sudan", | |
| "Suriname", | |
| "Swaziland", | |
| "Sweden", | |
| "Switzerland", | |
| "Syria", | |
| "Taiwan", | |
| "Tajikistan", | |
| "Tanzania", | |
| "Thailand", | |
| "Timor L'Este", | |
| "Togo", | |
| "Tonga", | |
| "Trinidad & Tobago", | |
| "Tunisia", | |
| "Turkey", | |
| "Turkmenistan", | |
| "Turks & Caicos", | |
| "Tuvalu", | |
| "Uganda", | |
| "Ukraine", | |
| "United Arab Emirates", | |
| "United Kingdom", | |
| "United States of America", | |
| "Uruguay", | |
| "Uzbekistan", | |
| "Vanuatu", | |
| "Vatican City", | |
| "Venezuela", | |
| "Vietnam", | |
| "Virgin Islands (US)", | |
| "Yemen", | |
| "Zambia", | |
| "Zimbabwe", | |
| ]; | |
| const searchInput = document.getElementById("search"); | |
| searchInput.oninput = onInput; | |
| const autoCompleteItemsElement = document.getElementById( | |
| "auto-complete-items" | |
| ); | |
| function onInput(e) { | |
| const { value } = e.target; | |
| console.log(value, e.target); | |
| const autoCompleteList = countries.filter((country) => { | |
| return country.toLocaleLowerCase().startsWith(value); | |
| }); | |
| // Fastest way. | |
| if (value.length === 0) { | |
| autoCompleteItemsElement.textContent = ""; | |
| return; | |
| } | |
| // Render items. | |
| const elementsToRender = []; | |
| for (const item of autoCompleteList) { | |
| const element = document.createElement("div"); | |
| element.id = item; | |
| element.textContent = item; | |
| elementsToRender.push(element); | |
| } | |
| autoCompleteItemsElement.replaceChildren(...elementsToRender); | |
| } | |
| </script> | |
| </body> | |
| </html> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gistpreview.github.io/?b85b2733b1c568f4fed3e22176470911