LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » JavaScript Based AutoComplete DropDown Using Pure Vanilla JavaScript

JavaScript Based AutoComplete DropDown Using Pure Vanilla JavaScript

JavaScript-Based-AutoComplete-DropDown-Using-Pure-Vanilla-JavaScript
This Vanilla JavaScript code snippet helps you to create autocomplete suggestion dropdown. It uses JavaScript regular expression pattern to match the entered value on keyup event. After matching, it appends the result to the suggestions list and highlights both the first letter and all matches in the suggested keywords.

You just need to update your keywords in the array according to your select menu. Then this plugin will search and load the relevant result in the suggestions list. Besides this, you can customize/style the select dropdown with additional CSS according to your needs.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Features:

  1. Light Weight.
  2. Pure JavaScript.
  3. Cross Browser.
  4. No JQuery Files.
  5. Fully Customizable.
  6. Responsive.

How To Add Pure Vanilla JavaScript Based AutoComplete DropDown ?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

Recommended For You:
Add Simple Hidden Search Widget With Toggle Using Pure Vanilla JavaScript

CSS:

<style type="text/css">
input {
border: 0 none;
}

.search-container {
width: 400px;
position: relative;
margin: 15px auto;
}
.search-container input,
.search-container .suggestions {
width: 100%;
background: #fff;
text-align: left;
}
.search-container input {
background: rgba(255, 255, 255, 0.2);
height: 60px;
padding: 0 10px;
}
.search-container .suggestions {
position: absolute;
top: 60px;
}

ul {
display: none;
list-style-type: none;
padding: 0;
margin: 0;
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.2);
max-height: 200px;
overflow-y: auto;
}
ul.has-suggestions {
display: block;
}
ul li {
padding: 10px;
cursor: pointer;
background: rgba(255, 255, 255, 0.2);
}
ul li:hover {
background-color: #e65c00;
}

input {
border-bottom: 2px solid #e65c00;
}
</style>

HTML:

<div class="search-container">
<input type="text" name="fruit" id="fruit" placeholder="Search fruit 🍎">
<div class="suggestions">
<ul></ul>
</div>
</div>

JavaScript:

<script type="text/javascript">
const input = document.querySelector('#fruit');
const suggestions = document.querySelector('.suggestions ul');

const fruit = [ 'Apple', 'Apricot', 'Avocado 🥑', 'Banana', 'Bilberry', 'Blackberry', 'Blackcurrant', 'Blueberry', 'Boysenberry', 'Currant', 'Cherry', 'Coconut', 'Cranberry', 'Cucumber', 'Custard apple', 'Damson', 'Date', 'Dragonfruit', 'Durian', 'Elderberry', 'Feijoa', 'Fig', 'Gooseberry', 'Grape', 'Raisin', 'Grapefruit', 'Guava', 'Honeyberry', 'Huckleberry', 'Jabuticaba', 'Jackfruit', 'Jambul', 'Juniper berry', 'Kiwifruit', 'Kumquat', 'Lemon', 'Lime', 'Loquat', 'Longan', 'Lychee', 'Mango', 'Mangosteen', 'Marionberry', 'Melon', 'Cantaloupe', 'Honeydew', 'Watermelon', 'Miracle fruit', 'Mulberry', 'Nectarine', 'Nance', 'Olive', 'Orange', 'Clementine', 'Mandarine', 'Tangerine', 'Papaya', 'Passionfruit', 'Peach', 'Pear', 'Persimmon', 'Plantain', 'Plum', 'Pineapple', 'Pomegranate', 'Pomelo', 'Quince', 'Raspberry', 'Salmonberry', 'Rambutan', 'Redcurrant', 'Salak', 'Satsuma', 'Soursop', 'Star fruit', 'Strawberry', 'Tamarillo', 'Tamarind', 'Yuzu'];

function search(str) {
let results = [];
const val = str.toLowerCase();

for (i = 0; i < fruit.length; i++) {
if (fruit[i].toLowerCase().indexOf(val) > -1) {
results.push(fruit[i]);
}
}

return results;
}

function searchHandler(e) {
const inputVal = e.currentTarget.value;
let results = [];
if (inputVal.length > 0) {
results = search(inputVal);
}
showSuggestions(results, inputVal);
}

function showSuggestions(results, inputVal) {

suggestions.innerHTML = '';

if (results.length > 0) {
for (i = 0; i < results.length; i++) {
let item = results[i];
// Highlights only the first match
// TODO: highlight all matches
const match = item.match(new RegExp(inputVal, 'i'));
item = item.replace(match[0], `<strong>${match[0]}</strong>`);
suggestions.innerHTML += `<li>${item}</li>`;
}
suggestions.classList.add('has-suggestions');
} else {
results = [];
suggestions.innerHTML = '';
suggestions.classList.remove('has-suggestions');
}
}

function useSuggestion(e) {
input.value = e.target.innerText;
input.focus();
suggestions.innerHTML = '';
suggestions.classList.remove('has-suggestions');
}

input.addEventListener('keyup', searchHandler);
suggestions.addEventListener('click', useSuggestion);
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Recommended For You:
5 Stylish And Awesome Pure CSS Tooltip Widgets

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *