Search Filter feature using HTML, CSS and JavaScript

Search Filter | Coding Zemigle
In today's digital landscape, user experience is paramount, and one key element that enhances usability is the search filter feature. A search filter allows users to quickly and efficiently narrow down a list of items or data based on specific criteria. This feature is crucial for applications with large datasets, such as e-commerce sites, content libraries, or any platform where users need to sift through numerous options.

In this tutorial, we'll walk through the process of creating a search filter using HTML, CSS, and JavaScript. We'll start by setting up the basic structure of our project, including the HTML for the search input and the items to be filtered. Then, we'll apply CSS to style our elements and ensure a polished look. Finally, we'll dive into JavaScript to add the functionality that will enable real-time filtering of the items based on user input.

Step 01: Setting Up the Project

To create a functional search filter, you'll need to set up a basic project structure that includes HTML for the content, CSS for styling, and JavaScript for the interactive functionality.

Start by creating an index.html file that will serve as the foundation of your project. This file will include the search input field and the list of items you want to filter.

✲    index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Search Filter | Coding Zemigle</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="search-container">
        <div class="search-filters">
            <input type="text" id="searchInput" placeholder="Search by program...">
            <select id="categorySelect">
                <option value="">All Categories</option>
                <option value="web">Web Development</option>
                <option value="mobile">Mobile Development</option>
                <option value="data">Data Science</option>
            </select>

            <select id="levelSelect">
                <option value="">All Levels</option>
                <option value="beginner">Beginner</option>
                <option value="intermediate">Intermediate</option>
                <option value="advanced">Advanced</option>
            </select>

            <label>
                <input type="checkbox" id="featuredOnly"/>
                Featured</label>

            <button onclick="filterResults()">Search</button>
        </div>

        <div id="results" class="results">
            <!-- Filtered results will appear here -->
        </div>
    </div>

    <script src="script.js"></script>
</body>
</html>



This HTML structure sets up a straightforward search filter interface, with a search input field and a list of items that can be filtered based on the user's input. It provides a solid foundation for adding CSS and JavaScript to enhance functionality and styling.


You'll have a solid foundation for your search filter feature. From here, you can enhance and customize the functionality and design to better suit your specific needs.

Step 02: Styling the project with CSS

CSS (Cascading Style Sheets) is used to control the visual appearance of your HTML elements. For the search filter feature, you'll need to style both the search input field and the list of items to create a clean, user-friendly interface. Below are some basic styles to get you started.   

✲    style.css
@import url('https://fonts.googleapis.com/css?family=Verdana:400,500,600,700&display=swap');

body {
    font-family: "Verdana", sans-serif;
    background-color: #677AFF;
    margin: 0;
    padding: 20px;
}

.search-container {
    max-width: 800px;
    margin: 0 auto;
    background-color: #fff;
    padding: 20px;
    border-radius: 5px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

h2 {
    text-align: center;
    margin-bottom: 20px;
}

.search-filters {
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
}

.search-filters input[type="text"] {
    flex: 1;
    padding: 15px;
    font-weight: 500;
    font-size: 15px;
    border: 1px solid #ccc;
    border-radius: 4px;
}

.search-filters select{
    flex: 1;
    padding: 12px;
    font-size: 15px;
    border: 1px solid #ccc;
    border-radius: 4px;
}

.search-filters label {
    display: flex;
    align-items: center;
    margin-right: 20px;
    font-size: 15px;
}

label input{
  margin-right: 10px;
}

.search-filters button {
    padding: 12px 20px;
    background-color: #677AFF;
    color: #fff;
    border: none;
    width: 100%;
    max-width: 150px;
    font-weight: bold;
    letter-spacing: 0.4px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 16px;
}

.results {
    margin-top: 20px;
}

.result-item {
    background-color: #f9f9f9;
    padding: 15px;
    border: 1px solid #ddd;
    margin-bottom: 10px;
    border-radius: 4px;
}

.result-item h3 {
    margin: 0 0 10px;
    font-size: 18px;
}

.result-item p {
    margin: 0;
    font-size: 14px;
    font-weight: 600;
    color: #555;
}



This CSS provides a clean and responsive design for the search filter interface, ensuring that the input field and list items are visually appealing and easy to use. Adjust the styles as needed to match the design and functionality of your specific project.


Step 03: Search Filter's JavaScript Functionality

JavaScript will handle the dynamic aspect of your search filter, allowing users to see the filtered results in real-time as they type. Here’s how you can implement this functionality:

✲    script.js
const data = [
    { id: 1, name: "HTML Basics", category: "web", level: "beginner", featured: true },
    { id: 2, name: "Advanced CSS Techniques", category: "web", level: "advanced", featured: false },
    { id: 3, name: "JavaScript for Beginners", category: "web", level: "beginner", featured: true },
    { id: 4, name: "Mobile App Development with React Native", category: "mobile", level: "intermediate", featured: false },
    { id: 5, name: "Data Science with Python", category: "data", level: "intermediate", featured: true },
    { id: 6, name: "Full-Stack Web Development", category: "web", level: "advanced", featured: false },
];

function filterResults() {
    const searchInput = document.getElementById('searchInput').value.toLowerCase();
    const categorySelect = document.getElementById('categorySelect').value;
    const levelSelect = document.getElementById('levelSelect').value;
    const featuredOnly = document.getElementById('featuredOnly').checked;

    const filteredData = data.filter(item => {
        const matchesName = item.name.toLowerCase().includes(searchInput);
        const matchesCategory = categorySelect === "" || item.category === categorySelect;
        const matchesLevel = levelSelect === "" || item.level === levelSelect;
        const matchesFeatured = !featuredOnly || item.featured;

        return matchesName && matchesCategory && matchesLevel && matchesFeatured;
    });

    displayResults(filteredData);
}

function displayResults(results) {
    const resultsContainer = document.getElementById('results');
    resultsContainer.innerHTML = '';

    if (results.length === 0) {
        resultsContainer.innerHTML = '<p>No results found.</p>';
        return;
    }

    results.forEach(item => {
        const resultItem = document.createElement('div');
        resultItem.className = 'result-item';

        resultItem.innerHTML = `
            <h3>${item.name}</h3>
            <p>Category: ${item.category.charAt(0).toUpperCase() + item.category.slice(1)}</p>
            <p>Level: ${item.level.charAt(0).toUpperCase() + item.level.slice(1)}</p>
            <p>Featured: ${item.featured ? 'Yes' : 'No'}</p>
        `;

        resultsContainer.appendChild(resultItem);
    });
}


This JavaScript code will enable the search filter functionality, dynamically showing and hiding items based on the user’s input. Adjust and expand the code as needed to fit the specific requirements and design of your project.

Conclusion

Implementing a search filter feature significantly enhances the usability of web applications by allowing users to quickly and easily locate specific items from a larger dataset. This guide has walked you through the process of creating a search filter using HTML, CSS, and JavaScript, covering everything from setting up the project and defining the HTML structure to styling the interface and adding interactive functionality.

Remember, effective testing and debugging are crucial to ensure that your search filter works flawlessly across different scenarios and devices. Continuous refinement based on user feedback will help you maintain a high-quality, user-friendly search experience.

With these tools and knowledge, you can now integrate a search filter into your own projects, providing a more intuitive and efficient way for users to find what they're looking for.