* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #FFCBA4 0%, #FFA07A 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.container {
width: 100%;
max-width: 500px;
}
.autocomplete-wrapper {
position: relative;
width: 100%;
}
.input-container {
position: relative;
width: 100%;
}
#autocomplete-input {
width: 100%;
padding: 16px 20px;
font-size: 16px;
border: none;
border-radius: 12px;
background: white;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
outline: none;
transition: all 0.3s ease;
}
#autocomplete-input:focus {
box-shadow: 0 6px 30px rgba(0, 0, 0, 0.15);
transform: translateY(-2px);
}
#autocomplete-input::placeholder {
color: #aaa;
}
.suggestions-list {
position: absolute;
top: calc(100% + 8px);
left: 0;
width: 100%;
background: white;
border-radius: 12px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
max-height: 300px;
overflow-y: auto;
display: none;
z-index: 1000;
}
.suggestions-list.active {
display: block;
}
.suggestion-item {
padding: 14px 20px;
cursor: pointer;
transition: background-color 0.2s ease;
border-bottom: 1px solid #f0f0f0;
}
.suggestion-item:last-child {
border-bottom: none;
}
.suggestion-item:hover,
.suggestion-item.highlighted {
background: linear-gradient(135deg, #FFCBA4 0%, #FFD4B8 100%);
}
.suggestion-item mark {
background-color: #FFE4CC;
font-weight: 600;
padding: 2px 0;
}
.no-results {
padding: 14px 20px;
color: #999;
text-align: center;
}
.label {
display: block;
margin-bottom: 10px;
color: white;
font-size: 18px;
font-weight: 500;
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
const countries = [
'Afghanistan', 'Albania', 'Algeria', 'Andorra', 'Argentina', 'Australia',
'Austria', 'Bangladesh', 'Belgium', 'Brazil', 'Bulgaria', 'Canada',
'China', 'Colombia', 'Croatia', 'Cuba', 'Denmark', 'Egypt',
'Finland', 'France', 'Germany', 'Greece', 'Hungary', 'Iceland',
'India', 'Indonesia', 'Iran', 'Iraq', 'Ireland', 'Israel',
'Italy', 'Japan', 'Jordan', 'Kenya', 'Kuwait', 'Lebanon',
'Malaysia', 'Mexico', 'Morocco', 'Nepal', 'Netherlands', 'New Zealand',
'Nigeria', 'Norway', 'Pakistan', 'Peru', 'Philippines', 'Poland',
'Portugal', 'Qatar', 'Romania', 'Russia', 'Saudi Arabia', 'Singapore',
'South Africa', 'South Korea', 'Spain', 'Sweden', 'Switzerland', 'Syria',
'Thailand', 'Turkey', 'Ukraine', 'United Arab Emirates', 'United Kingdom',
'United States', 'Venezuela', 'Vietnam', 'Yemen'
];
const input = document.getElementById('autocomplete-input');
const suggestionsList = document.getElementById('suggestions-list');
let currentFocus = -1;
input.addEventListener('input', function(e) {
const val = this.value.trim();
currentFocus = -1;
if (!val) {
suggestionsList.classList.remove('active');
return;
}
const matches = countries.filter(country =>
country.toLowerCase().includes(val.toLowerCase())
);
if (matches.length === 0) {
suggestionsList.innerHTML = '<div class="no-results">No results found</div>';
suggestionsList.classList.add('active');
return;
}
suggestionsList.innerHTML = matches.map(country => {
const regex = new RegExp(`(${val})`, 'gi');
const highlighted = country.replace(regex, '<mark>$1</mark>');
return `<div class="suggestion-item" data-value="${country}">${highlighted}</div>`;
}).join('');
suggestionsList.classList.add('active');
});
suggestionsList.addEventListener('click', function(e) {
if (e.target.classList.contains('suggestion-item')) {
input.value = e.target.dataset.value;
suggestionsList.classList.remove('active');
}
});
input.addEventListener('keydown', function(e) {
const items = suggestionsList.getElementsByClassName('suggestion-item');
if (e.key === 'ArrowDown') {
e.preventDefault();
currentFocus++;
if (currentFocus >= items.length) currentFocus = 0;
setActive(items);
} else if (e.key === 'ArrowUp') {
e.preventDefault();
currentFocus--;
if (currentFocus < 0) currentFocus = items.length - 1;
setActive(items);
} else if (e.key === 'Enter') {
e.preventDefault();
if (currentFocus > -1 && items[currentFocus]) {
input.value = items[currentFocus].dataset.value;
suggestionsList.classList.remove('active');
}
} else if (e.key === 'Escape') {
suggestionsList.classList.remove('active');
}
});
function setActive(items) {
Array.from(items).forEach(item => item.classList.remove('highlighted'));
if (items[currentFocus]) {
items[currentFocus].classList.add('highlighted');
items[currentFocus].scrollIntoView({ block: 'nearest' });
}
}
document.addEventListener('click', function(e) {
if (!e.target.closest('.autocomplete-wrapper')) {
suggestionsList.classList.remove('active');
}
});
No comments yet. Be the first!