The search bar is an important feature for any website. Help users quickly find the content they want. In this tutorial Instead of delving into complex search algorithms, we’ll create a simple but powerful search bar using only HTML, CSS, and JavaScript. We focused on creating a user-friendly tool to search for specific words or phrases within a list of objects.
Project Overview
In this project, we will create a search bar that filters the list of animals in real time. When typing in the search bar The user will only see items that match the information entered. This is a great beginner-friendly project to learn the basics of web development.
Features
- Interactive search: Filter inventory items based on user input.
- Responsive design: Compatible with various screen sizes.
- Stylish Visuals: Animations and transitions for an engaging user experience.
Implementation Steps
- HTML Setup
Create a container for the search bar and create an ordered list. Each item shows the name of the animal. This is the basic structure:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Search Bar Example</title>
<link rel="stylesheet" type="text/css" href="./style.css">
</head>
<body>
<div class="container">
<input id="searchbar"
onkeyup="search_animal()"
type="text"
placeholder="Search animals...">
<ul id="list">
<li class="animals">Cat</li>
<li class="animals">Dog</li>
<li class="animals">Elephant</li>
<li class="animals">Fish</li>
<li class="animals">Gorilla</li>
<li class="animals">Monkey</li>
<li class="animals">Turtle</li>
<li class="animals">Whale</li>
<li class="animals">Alligator</li>
<li class="animals">Donkey</li>
<li class="animals">Horse</li>
</ul>
</div>
<script src="./script.js"></script>
</body>
</html>
- Styling with CSS
Add styles to enhance the visual appeal. Use a .container
for layout, design the input box, and style the list for a neat appearance.
/* style.css */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f4f4f4;
}
.container {
width: 80%;
max-width: 600px;
}
#searchbar {
width: 100%;
padding: 10px;
font-size: 16px;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 4px;
}
#list {
list-style-type: none;
padding: 0;
}
.animals {
padding: 10px;
border: 1px solid #ddd;
margin-bottom: 5px;
border-radius: 4px;
transition: all 0.3s ease;
}
.animals:hover {
background-color: #f0f0f0;
}
- Adding JavaScript Functionality
Use JavaScript to filter the list based on user input.
// script.js
function search_animal() {
const input = document.getElementById('searchbar').value.toLowerCase();
const items = document.querySelectorAll('.animals');
items.forEach(item => {
if (item.innerText.toLowerCase().includes(input)) {
item.style.display = '';
} else {
item.style.display = 'none';
}
});
}
Live Output
Typing in the search bar will instantly filter the list. It only shows animals whose names match the input.
Why This Project?
- Hands-on learning: Understand the basics of DOM manipulation and event handling.
- Real World Applications: Create essential features for websites.
- Customizable: This functionality can be easily customized for large projects.
Next Steps
Expand on this project by:
- Adding animations for smooth transitions.
- Integrating advanced search features like fuzzy matching.
- Creating a dropdown suggestion list based on input.