Here's a complete index.html file that implements a multi-select dropdown component with search functionality. The code includes HTML, CSS for styling, and JavaScript for the interactive behavior, all embedded within the same file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multi-Select Dropdown with Search</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.dropdown {
position: relative;
display: inline-block;
width: 300px;
}
.dropdown-toggle {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
cursor: pointer;
background-color: #f9f9f9;
display: flex;
justify-content: space-between;
align-items: center;
}
.dropdown-menu {
display: none;
position: absolute;
background-color: white;
border: 1px solid #ccc;
border-radius: 4px;
width: 100%;
max-height: 200px;
overflow-y: auto;
z-index: 1000;
}
.dropdown-menu.show {
display: block;
}
.dropdown-menu input {
width: 100%;
padding: 10px;
border: none;
border-bottom: 1px solid #ccc;
}
.dropdown-item {
padding: 10px;
cursor: pointer;
}
.dropdown-item:hover {
background-color: #f0f0f0;
}
.selected-items {
margin-top: 10px;
padding: 5px;
border: 1px solid #ccc;
border-radius: 4px;
min-height: 30px;
display: flex;
flex-wrap: wrap;
}
.tag {
background-color: #007bff;
color: white;
padding: 5px 10px;
border-radius: 15px;
margin: 2px;
}
</style>
</head>
<body>
<div class="dropdown">
<div class="dropdown-toggle" id="dropdownToggle">Select Items <span>▼</span></div>
<div class="dropdown-menu" id="dropdownMenu">
<input type="text" id="searchInput" placeholder="Search...">
<div class="dropdown-item" data-value="Option 1">Option 1</div>
<div class="dropdown-item" data-value="Option 2">Option 2</div>
<div class="dropdown-item" data-value="Option 3">Option 3</div>
<div class="dropdown-item" data-value="Option 4">Option 4</div>
<div class="dropdown-item" data-value="Option 5">Option 5</div>
</div>
<div class="selected-items" id="selectedItems"></div>
</div>
<script>
const dropdownToggle = document.getElementById('dropdownToggle');
const dropdownMenu = document.getElementById('dropdownMenu');
const searchInput = document.getElementById('searchInput');
const selectedItemsContainer = document.getElementById('selectedItems');
const dropdownItems = document.querySelectorAll('.dropdown-item');
let selectedItems = [];
dropdownToggle.addEventListener('click', () => {
dropdownMenu.classList.toggle('show');
});
dropdownItems.forEach(item => {
item.addEventListener('click', () => {
const value = item.getAttribute('data-value');
if (selectedItems.includes(value)) {
selectedItems = selectedItems.filter(item => item !== value);
item.classList.remove('selected');
} else {
selectedItems.push(value);
item.classList.add('selected');
}
updateSelectedItems();
});
});
searchInput.addEventListener('input', () => {
const filter = searchInput.value.toLowerCase();
dropdownItems.forEach(item => {
const text = item.textContent.toLowerCase();
item.style.display = text.includes(filter) ? 'block' : 'none';
});
});
document.addEventListener('click', (event) => {
if (!dropdownToggle.contains(event.target) && !dropdownMenu.contains(event.target)) {
dropdownMenu.classList.remove('show');
}
});
function updateSelectedItems() {
selectedItemsContainer.innerHTML = '';
selectedItems.forEach(item => {
const tag = document.createElement('div');
tag.className = 'tag';
tag.textContent = item;
selectedItemsContainer.appendChild(tag);
});
}
</script>
</body>
</html>
Explanation:
- HTML Structure: The dropdown consists of a toggle button, a menu with a search input, and a container to display selected items.
- CSS Styles: The styles are designed to make the dropdown visually appealing and user-friendly.
- JavaScript Logic:
- Toggles the visibility of the dropdown menu when the toggle button is clicked.
- Allows multiple selections of items and updates the display of selected items.
- Filters the dropdown items based on the search input.
- Closes the dropdown when clicking outside of it.
You can copy and paste this code into an index.html file and open it in a web browser to see the multi-select dropdown in action.