let suggestions = [
'javascript',
'css',
'scss',
'jquery',
'php',
'python',
'node.js',
'html5'
];
let input = document.querySelector('.search input');
let searchBox = document.querySelector('.search-box');
let suggestionList = document.querySelector('.suggestions');
input.addEventListener('focus', function(e) {
//let suggestions = JSON.parse(sessionStorage.getItem('suggestions'));
fillSuggestionList(suggestions);
searchBox.classList.add('visible');
});
input.addEventListener('blur', function(e) {
searchBox.classList.remove('visible');
clearSuggestionList();
});
input.addEventListener('input', function(e) {
let enteredText = this.value.toLowerCase();
let suggestions = suggestionList.children;
[].forEach.call(suggestions, function(suggestion) {
let suggestionContent = suggestion.textContent;
if (enteredText.length && suggestionContent.includes(enteredText)) {
suggestion.style.display = 'block';
} else {
suggestion.style.display = 'none';
}
});
});
function fillSuggestionList(suggestions) {
suggestions.forEach(function(suggestion) {
let li = document.createElement('li');
li.textContent = suggestion;
suggestionList.appendChild(li);
});
}
function clearSuggestionList() {
while (suggestionList.firstChild) {
suggestionList.removeChild(suggestionList.firstChild);
}
}
@import url('https://fonts.googleapis.com/css?family=Noto+Sans:400,700');
*,
*:before,
*:after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
background-color: #fff;
}
.input {
border: 1px solid #ccc;
border-radius: 3px;
box-shadow: 0 1px 3px 0 rgba(0, 0, 0, .1) inset;
color: #555;
font-size: 14px;
height: 35px;
padding: 0 .75rem;
transition: all .25s var(--ease);
width: 100%;
}
.input:focus {
border-color: rgba(0, 100, 255, .5);
outline: none;
}
.input-addon {
display: flex;
flex-direction: row-reverse;
}
.input-addon .icon {
align-items: center;
border: 1px solid #ccc;
border-right: 0;
border-radius: 3px 0 0 3px;
box-shadow: inset 0 0.0625rem 0.1875rem 0 rgba(0, 0, 0, .1);
display: flex;
flex: 0 0 35px;
height: 35px;
justify-content: center;
margin: 0;
position: relative;
transition: all .25s var(--ease);
}
.input-addon .icon:after {
background-color: #fff;
bottom: 1px;
content: "";
height: 95%;
position: absolute;
right: -1px;
width: 2px;
}
.input-addon .icon i {
color: #777;
}
.input-addon .input {
border-left: none;
border-radius: 0 3px 3px 0;
flex: 1;
}
.search {
margin: 20px auto;
position: relative;
width: 80%;
}
.input-addon .input:focus+.icon {
border-color: rgba(0, 100, 250, .5);
}
.search-box {
border-radius: 3px;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .23), 0 -1px 1px 0 rgba(0, 0, 0, .1);
padding: 8px;
opacity: 0;
position: absolute;
top: 100%;
transition: all .25s linear;
visibility: hidden;
width: 100%;
}
.search-box.visible {
opacity: 1;
top: 140%;
visibility: visible;
}
.suggestions {
list-style: none;
padding: 0;
}
.suggestions li {
color: #555;
display: none;
font-family: 'Noto Sans';
font-size: 14px;
padding: 10px 12px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="search">
<div class="input-addon">
<input type="search" class="input">
<figure class="icon">
<i class="fa fa-search"></i>
</figure>
</div>
<div class="search-box">
<ul class="suggestions">
</ul>
</div>
</div>