Coder Wrap provides free to use snippets Coder Wrap provides free to use snippets

🗙
Login
Register

Create Account

Cancel
Back to Common
Preview Source Code
Download
HTML
CSS
* {
 box-sizing: border-box;
}

body {
 margin: 0px;
}

.search-box {
 max-width: 400px;
 margin: 30px auto;
 position: relative;
}

.search-box:before {
 content: '';
 position: absolute;
 width: 10px;
 height: 10px;
 border-radius: 100%;
 right: 15px;
 top: 12px;
 border: #ccc 2px solid;
 cursor: pointer;
}

.search-box:after {
 content: '';
 position: absolute;
 width: 3px;
 height: 10px;
 right: 16px;
 bottom: 10px;
 transform: rotate(-45deg);
 background-color: #ccc;
 cursor: pointer;
}

.search-box input {
 border: #ccc 1px solid;
 font-size: 15px;
 padding: 12px 24px 12px 12px;
 width: 100%;
 outline: none;
}

.listbox {
 max-width: 1200px;
 margin: auto;
 width: 100%;
 font-family: system-ui;
 display: flex;
 flex-flow: row wrap;
}

.listbox .item {
 width: calc(33.33% - 20px);
 margin: 10px;
 border: #e1e1e1 1px solid;
 padding: 20px;
 box-shadow: #dbdbdb 0px 0px 8px;
}

.listbox .item h4 {
 margin: 0px 0px 8px 0px;
 font-size: 20px;
}

.listbox .item p {
 margin: 0px;
}

.item .readmore-btn {
 border: #ccc 1px solid;
 padding: 5px 10px;
 line-height: normal;
 text-decoration: none;
 margin-top: 15px;
 font-weight: 700;
 font-size: 14px;
 border-radius: 4px;
 display: inline-block;
 color: #000;
 cursor: pointer;
}

.datanofound {
 width: 100%;
 text-align: center;
 display: none;
 font-weight: 400;
 color: #000;
 font-size: 16px;
 padding: 20px 0;
 font-family: system-ui;
}

@media(max-width:992px) {
 .listbox .item {
  width: calc(50% - 10px);
  margin: 5px;
  padding: 10px;
  font-size: 14px;
 }

 .listbox .item h4 {
  font-size: 15px;
 }

}
JS
// Get the input element and the FAQ items
const searchInput = document.getElementById('searchBar');
const boxItems = document.querySelectorAll('.listbox .item');
const noResultsDiv = document.querySelector('.datanofound'); // Get the "Data Not Found" element
// Initialize foundResults as false
let foundResults = false;
// Add an event listener to the search input
searchInput.addEventListener('input', function () {
 const searchTerm = searchInput.value.toLowerCase();
 // Reset foundResults to false when a new search is initiated
   foundResults = false;
  boxItems.forEach(function (boxItem) {
      const itemTitle = boxItem.querySelector('h4').textContent.toLowerCase();
      const itemDesc = boxItem.querySelector('p').textContent.toLowerCase();

      if (itemTitle.includes(searchTerm) || itemDesc.includes(searchTerm)) {
          boxItem.style.display = 'block';
          foundResults = true; 
      } else {
          boxItem.style.display = 'none';
      }
  });
  // Finally, check foundResults to show/hide the "Data Not Found" message
  if (foundResults) {
      noResultsDiv.style.display = 'none';
  } else {
      noResultsDiv.style.display = 'block';
  }
});

Related Snippets

Leave a comment

Your email address will not be published. Required fields are marked *