Show search results only when searching

Viewed 34

How can I hide the list of products, and show the results only when searching using the search bar?

Every time I hide the list of products, when I search the entire list appears and not only the search result.

I want to show only the search result when searching using the search bar.

function search() {
  var searchBar, ProductsList, Products, ProductsTitle, TextValue, Element


  searchBar = document.getElementById('searchBar').value.toUpperCase()
  ProductsList = document.getElementById('product-list')
  Products = document.querySelectorAll('.product')
  ProductsTitle = ProductsList.getElementsByTagName('h2')

  for (let i = 0; i < ProductsTitle.length; i++) {
    TextValue = Products[i].getElementsByTagName('h2')[0];
    if (TextValue) {
      Element = TextValue.textContent || e.innerHTML
      if (Element.toUpperCase().indexOf(searchBar) > -1) {
        Products[i].style.display = ""
      } else {
        Products[i].style.display = "none"
      }
    }
  }
}
.search-result {
  max-width: max-content;
  background-color: rgba(0, 0, 0, 0.6);
  height: max-content;
}

.products {
  padding: 10px;
  width: 100%;
  display: flex;
  flex-direction: column;
  position: absolute;
  top: 50px;
  z-index: 2;
}

.product {
  float: right;
  display: flex;
  align-items: center;
  direction: rtl;
  color: white;
  background-color: rgba(0, 0, 0, 0.6);
  border-radius: 6px;
  border-bottom: 2px solid red;
}

.product text {
  float: left;
}

* {
  padding: 0;
  margin: 0;
  font-family: sans-serif;
}

.search {
  width: 100%;
  display: flex;
  justify-content: center;
  padding: 20px;
}

.search input {
  border-radius: 20px 0px 0px 20px;
  outline: none;
  width: 250px;
  border-color: red;
  padding-left: 20px;
}

.search button {
  border-radius: 0px 20px 20px 0px;
  padding: 5px 20px;
  background-color: red;
  color: white;
  font-size: 1.2rem;
  border-color: red;
  cursor: pointer;
  outline: none;
  border: none;
}
<div class="search">
  <input id="searchBar" type="text" placeholder="Search" onkeyup="search()">
  <button type="submit">search</button>
</div>
<div class="search-result">
</div>
<div class="img">
  <img src="https://www.sare.org/wp-content/uploads/Our-Farms-Our-Future-Podcast-Logo-330x166.jpg" style="width: 100%;" alt="">
</div>
<div class="products" id="product-list">
  <div class="product">
    <img src="images/img/2.png" alt="" style="width:150px;">
    <div class="text">
      <h2>Mango</h2>
      <h4>1.50€</h4>
    </div>
  </div>
  <div class="product">
    <img src="images/img/3.png" alt="" style="width:150px;">
    <div class="text">
      <h2>pomegranate</h2>
      <h4>1.50€</h4>
    </div>
  </div>
  <div class="product">
    <img src="images/img/4.png" alt="" style="width:150px;">
    <div class="text">
      <h2>Grape</h2>
      <h4>1.50€</h4>
    </div>
  </div>
  <div class="product">
    <img src="images/img/5.png" alt="" style="width:150px;">
    <div class="text">
      <h2>apple</h2>
      <h4>1.50€</h4>
    </div>
  </div>
  <div class="product">
    <img src="images/img/6.png" alt="" style="width:150px;">
    <div class="text">
      <h2>fig</h2>
      <h4>1.50€</h4>
    </div>
  </div>
</div>

1 Answers

You can use classList in parent .products

searchBarValue.length ? productsList.classList.add('active') : productsList.classList.remove('active')

When class is active the wrapper as display:flex

Note: I improved a bit your code

const searchBar = document.getElementById('searchBar')

function search() {

  const searchBarValue = document.getElementById('searchBar').value.toUpperCase()
  const productsList = document.getElementById('product-list')
  const products = document.querySelectorAll('.product')
  const productsTitle = productsList.getElementsByTagName('h2')

  searchBarValue.length ? productsList.classList.add('active') : productsList.classList.remove('active')

  for (let i = 0; i < productsTitle.length; i++) {
    const textValue = products[i].getElementsByTagName('h2')[0];
    if (textValue) {
      const element = textValue.textContent.toUpperCase()
      products[i].style.display = element.indexOf(searchBarValue) > -1 ? "" : "none"
    }
  }
}

searchBar.addEventListener('keyup', search)
.search-result {
  max-width: max-content;
  background-color: rgba(0, 0, 0, 0.6);
  height: max-content;
}

.products {
  padding: 10px;
  width: 100%;
  display: none;
  position: absolute;
  top: 50px;
  z-index: 2;
}

.products.active {
  display: flex;
  flex-direction: column;
}

.product {
  display: flex;
  align-items: center;
  direction: rtl;
  color: white;
  background-color: rgba(0, 0, 0, 0.6);
  border-radius: 6px;
  border-bottom: 2px solid red;
}

.product text {
  float: left;
}

* {
  padding: 0;
  margin: 0;
  font-family: sans-serif;
}

.search {
  width: 100%;
  display: flex;
  justify-content: center;
  padding: 20px;
}

.search input {
  border-radius: 20px 0px 0px 20px;
  outline: none;
  width: 250px;
  border-color: red;
  padding-left: 20px;
}

.search button {
  border-radius: 0px 20px 20px 0px;
  padding: 5px 20px;
  background-color: red;
  color: white;
  font-size: 1.2rem;
  border-color: red;
  cursor: pointer;
  outline: none;
  border: none;
}
<div class="search">
  <input id="searchBar" type="text" placeholder="Search">
  <button type="submit">search</button>
</div>
<div class="search-result">
</div>
<div class="img">
  <img src="https://www.sare.org/wp-content/uploads/Our-Farms-Our-Future-Podcast-Logo-330x166.jpg" style="width: 100%;" alt="">
</div>
<div class="products" id="product-list">
  <div class="product">
    <img src="images/img/2.png" alt="" style="width:150px;">
    <div class="text">
      <h2>Mango</h2>
      <h4>1.50€</h4>
    </div>
  </div>
  <div class="product">
    <img src="images/img/3.png" alt="" style="width:150px;">
    <div class="text">
      <h2>pomegranate</h2>
      <h4>1.50€</h4>
    </div>
  </div>
  <div class="product">
    <img src="images/img/4.png" alt="" style="width:150px;">
    <div class="text">
      <h2>Grape</h2>
      <h4>1.50€</h4>
    </div>
  </div>
  <div class="product">
    <img src="images/img/5.png" alt="" style="width:150px;">
    <div class="text">
      <h2>apple</h2>
      <h4>1.50€</h4>
    </div>
  </div>
  <div class="product">
    <img src="images/img/6.png" alt="" style="width:150px;">
    <div class="text">
      <h2>fig</h2>
      <h4>1.50€</h4>
    </div>
  </div>
</div>

Related