Given a list of items and the task is to filter through the items and return the best match using the search bar.
There are several approaches to implement this but, we will be using a simple if-else statement to implement our search bar.
Approach:
- Create a folder called gfgSearchBar.
- Open the folder in your desired IDE or IDLE.
- Create a html file called approachOne.html
- Paste the below code into the html file.
Example:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content= "width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <meta author="Emmanuel Onah" /> <title> GeeksforGeeks Search Bar </title> </head> <style> * { margin: 0; padding: 0; } .container { box-sizing: border-box; } header { display: flex; align-content: center; align-items: center; justify-content: space-evenly; background: #d87093; padding: 0.5rem 1rem; } h3 { color: #000; font-family: Arial, Helvetica, sans-serif; font-size: 0.98rem; } input[type='search'] { padding: 0.5rem 2rem 0.5rem 0.4rem; border-radius: 4rem; font-size: 0.98rem; } .movieCollection { width: 100%; margin: 0 auto; padding: 1rem; box-sizing: border-box; display: flex; flex-wrap: wrap; justify-content: space-between; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; } .movieWrapper { margin-top: 1.5rem; } img { width: 400px; height: 400px; display: block; } </style> <body> <div class="container"> <header> <h3>Geeks4Geeks Search Bar</h3> <input type="search" placeholder="search" id="searchBar" /> </header> <div class="movieCollection"> <div class="movieWrapper movieOne"> <h4>The city gate</h4> <time>01.02.1999</time> <img src= alt="The city gate" /> </div> <div class="movieWrapper movieTwo"> <h4>Land of scientist</h4> <time>01.02.2000</time> <img src= alt="Land of scientist" /> </div> <div class="movieWrapper movieThree"> <h4>Hidden treasure</h4> <time>01.02.2010</time> <img src= alt="Hidden treasure" /> </div> <div class="movieWrapper movieFour"> <h4>Beautiful city of heroes</h4> <time>01.02.2020</time> <img src= alt="Beautiful city of heroes" /> </div> <div class="movieWrapper movieFive"> <h4>The city gate</h4> <time>01.02.1999</time> <img src= alt="The city gate" /> </div> <div class="movieWrapper movieSix"> <h4>Land of scientist</h4> <time>01.02.2000</time> <img src= alt="Land of scientist" /> </div> </div> </div> <script> document.addEventListener('DOMContentLoaded', () => { const getSearchBar = document.querySelector('#searchBar'); const getAllMovies = document.querySelectorAll('.movieWrapper'); getSearchBar.addEventListener('keyup', e => { getAllMovies.forEach(movie => { if (movie.innerText.toLowerCase().includes( e.target.value.toLowerCase())) { movie.style.display = 'block'; return movie; } else { movie.style.display = 'none'; } }); }); }); </script> </body> </html> |
Output:

Explanation of the code inside the script tag:
getSearchBar.addEventListener(): We simply added a keyup event listener to the search bar.
getAllMovies.forEach(movie => {
if (movie.innerText.toLowerCase()
.includes(e.target.value.toLowerCase())) {
movie.style.display = 'block';
return movie;
}
else {
movie.style.display = 'none';
}
});
This block of code simply means the user types into the search bar are present in the movie text content, then style that movie box to be displayed in blocks and return all of them. Else don’t return any movie block.
Recommended Posts:
- How to add custom google search bar inside your web-page?
- Web 1.0, Web 2.0 and Web 3.0 with their difference
- JavaScript Course | Conditional Operator in JavaScript
- Search Bar using HTML, CSS and JavaScript
- Difference between self::$bar and static::$bar in PHP
- What are the differences between web-garden and a web-farm in JavaScript?
- Implementation of Binary Search Tree in Javascript
- Create a Search Bar using HTML and CSS
- How to get card vertically centered between search bar and bottom of the viewport?
- Web crawling using Breadth First Search at a specified depth
- Difference between Web Scraping and Web Crawling
- Difference between Web Browser and Web Server
- How to choose Web Hosting Server and Web Domain ?
- Difference between Web Designer and Web Developer
- Differences between Web Services and Web API
- Progressive Web App - A Combination of Native and Web App
- 10 Web Development and Web Design Facts That You Should Know
- Creating Progress Bar using JavaScript
- How to get the width of scroll bar using JavaScript ?
- How to set textarea scroll bar to bottom as a default using JavaScript/jQuery ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


