Introduction
In many websites, you can see the autocompletion of some names and places while you are typing. This is a concept of reducing the work for the users using it. It is more effective compared to the dropdown box since in the dropdown box, you need to search the name or place by scrolling down. But here, if you just type the first few letters of the field it will auto-fill it by giving you some suggestions. In this article, we can discuss the creation of a webpage with an auto-fill option for a country name with some autosuggestions.
Creating an Autocomplete Field
To create an autocomplete field, I am using four programming languages:
- HTML - For creating basic templates and functions.
- CSS - Attaching styles to the HTML template.
- JavaScript - For validation and triggering functions.
- PHP - For assigning button controls.
HTML and CSS are the two basic languages for creating this webpage. You can use some other languages instead of using PHP and JavaScript.
Steps
To create an autocomplete field, you can open a new HTML code editing page in your IDE. Here, I am using the Visual Studio Code for this purpose. You can install the Visual Studio Code by clicking
here. Here I embedded the codes in the single file, but you can link the different files for style sheet, Javascript, and PHP work.
You can use the source code given below for creating a page:
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <style>
- * {
- box-sizing: border-box;
- }
-
- body {
- font: 14px times_new_roman;
- }
-
-
- .autocomplete {
- position: relative;
- display: inline-block;
- }
-
- input {
- border: 1px solid transparent;
- background-color: #f1f1f1;
- padding: 10px;
- font-size: 16px;
- }
-
- input[type=text] {
- background-color: #f1f1f1;
- width: 100%;
- }
-
- input[type=submit] {
- background-color: rgb(78, 139, 200);
- color: #fff;
- cursor: pointer;
- }
-
- .autocomplete-items {
- position: absolute;
- border: 1px solid #d4d4d4;
- border-bottom: none;
- border-top: none;
- z-index: 99;
-
- top: 100%;
- left: 0;
- right: 0;
- }
-
- .autocomplete-items div {
- padding: 10px;
- cursor: pointer;
- background-color: #fff;
- border-bottom: 1px solid #d4d4d4;
- }
-
-
- .autocomplete-items div:hover {
- background-color: #e9e9e9;
- }
-
-
- .autocomplete-active {
- background-color: DodgerBlue !important;
- color: #ffffff;
- }
- </style>
- </head>
- <body>
-
- <h1>Autocomplete Of Country name </h1>
-
- <p>Start typing your country name:</p>
- <div class="autocomplete" style="width:300px;">
- <input id="myInput" type="text" name="myCountry" placeholder="Country">
- </div>
- <input type="submit">
- </form>
-
- <script>
- function autocomplete(inp, arr) {
-
-
- var currentFocus;
-
- inp.addEventListener("input", function(e) {
- var a, b, i, val = this.value;
-
- closeAllLists();
- if (!val) { return false;}
- currentFocus = -1;
-
- a = document.createElement("DIV");
- a.setAttribute("id", this.id + "autocomplete-list");
- a.setAttribute("class", "autocomplete-items");
-
- this.parentNode.appendChild(a);
-
- for (i = 0; i < arr.length; i++) {
-
- if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
-
- b = document.createElement("DIV");
-
- b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
- b.innerHTML += arr[i].substr(val.length);
-
- b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
-
- b.addEventListener("click", function(e) {
-
- inp.value = this.getElementsByTagName("input")[0].value;
-
-
- closeAllLists();
- });
- a.appendChild(b);
- }
- }
- });
-
- inp.addEventListener("keydown", function(e) {
- var x = document.getElementById(this.id + "autocomplete-list");
- if (x) xx = x.getElementsByTagName("div");
- if (e.keyCode == 40) {
-
-
- currentFocus++;
-
- addActive(x);
- } else if (e.keyCode == 38) {
-
-
- currentFocus--;
-
- addActive(x);
- } else if (e.keyCode == 13) {
-
- e.preventDefault();
- if (currentFocus > -1) {
-
- if (x) x[currentFocus].click();
- }
- }
- });
- function addActive(x) {
-
- if (!x) return false;
-
- removeActive(x);
- if (currentFocus >= x.length) currentFocus = 0;
- if (currentFocus < 0) currentFocus = (x.length - 1);
-
- x[currentFocus].classList.add("autocomplete-active");
- }
- function removeActive(x) {
-
- for (var i = 0; i < x.length; i++) {
- x[i].classList.remove("autocomplete-active");
- }
- }
- function closeAllLists(elmnt) {
-
-
- var x = document.getElementsByClassName("autocomplete-items");
- for (var i = 0; i < x.length; i++) {
- if (elmnt != x[i] && elmnt != inp) {
- x[i].parentNode.removeChild(x[i]);
- }
- }
- }
-
- document.addEventListener("click", function (e) {
- closeAllLists(e.target);
- });
- }
-
-
- var 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"];
-
-
- autocomplete(document.getElementById("myInput"), countries);
- </script>
-
- </body>
- </html>
Output Verification
After coding, you must save the code with .html. You can find an HTML page in the place where you saved the file.
Open that file in a browser. There you are able to see a field called country.
Start typing your country name there, it will automatically suggest the country name for you.
After choosing that, you can submit.
Conclusion
This is an option to help the users of the website. You can use this code while you are developing a website. You can load it into the server and then use it.