Skip to content
Advertisement

Hide search elements in Javascript

In the following example code I want to hide the elements that show under the search field. Code is from W3: https://www.w3schools.com/howto/howto_js_filter_lists.asp Search elements should only show when users starts typing in the search field. Nothing complex just looking for solution for beginner. Thanks

    <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
  box-sizing: border-box;
}

#myInput {
  background-image: url('/css/searchicon.png');
  background-position: 10px 12px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myUL {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

#myUL li a {
  border: 1px solid #ddd;
  margin-top: -1px; /* Prevent double borders */
  background-color: #f6f6f6;
  padding: 12px;
  text-decoration: none;
  font-size: 18px;
  color: black;
  display: block
}

#myUL li a:hover:not(.header) {
  background-color: #eee;
}
</style>
</head>
<body>

<h2>My Phonebook</h2>

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">

<ul id="myUL">
  <li><a href="#">Adele</a></li>
  <li><a href="#">Agnes</a></li>

  <li><a href="#">Billy</a></li>
  <li><a href="#">Bob</a></li>

  <li><a href="#">Calvin</a></li>
  <li><a href="#">Christina</a></li>
  <li><a href="#">Cindy</a></li>
</ul>

<script>
function myFunction() {
    var input, filter, ul, li, a, i, txtValue;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    ul = document.getElementById("myUL");
    li = ul.getElementsByTagName("li");
    for (i = 0; i < li.length; i++) {
        a = li[i].getElementsByTagName("a")[0];
        txtValue = a.textContent || a.innerText;
        if (txtValue.toUpperCase().indexOf(filter) > -1) {
            li[i].style.display = "";
        } else {
            li[i].style.display = "none";
        }
    }
}
</script>

</body>
</html>

Answer

Just set display: none to #myUL li

#myUL li {
  display: none;
}

and change in your js add li[i].style.display = "block"

if (txtValue.toUpperCase().indexOf(filter) > -1) {
  li[i].style.display = "block";
} else {
  li[i].style.display = "none";
}

Working Example

function myFunction() {
  var input, filter, ul, li, a, i, txtValue;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  ul = document.getElementById("myUL");
  li = ul.getElementsByTagName("li");
  for (i = 0; i < li.length; i++) {
    a = li[i].getElementsByTagName("a")[0];
    txtValue = a.textContent || a.innerText;
    if (txtValue.toUpperCase().indexOf(filter) > -1) {
      li[i].style.display = "block";
    } else {
      li[i].style.display = "none";
    }
  }
}
* {
  box-sizing: border-box;
}

#myInput {
  background-image: url('/css/searchicon.png');
  background-position: 10px 12px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myUL {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

#myUL li {
  display: none
}

#myUL li a {
  border: 1px solid #ddd;
  margin-top: -1px;
  /* Prevent double borders */
  background-color: #f6f6f6;
  padding: 12px;
  text-decoration: none;
  font-size: 18px;
  color: black;
  display: block
}

#myUL li a:hover:not(.header) {
  background-color: #eee;
}
<h2>My Phonebook</h2>

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">

<ul id="myUL">
  <li><a href="#">Adele</a></li>
  <li><a href="#">Agnes</a></li>

  <li><a href="#">Billy</a></li>
  <li><a href="#">Bob</a></li>

  <li><a href="#">Calvin</a></li>
  <li><a href="#">Christina</a></li>
  <li><a href="#">Cindy</a></li>
</ul>

My approach would be different for making this search bar

const input = document.querySelector("input");
const listItems = document.querySelectorAll(".list-item");

input.addEventListener("input", (event) => {
  const value = event.target.value;

  listItems.forEach((listItem) => {
    const target =
      value.trim().length > 0 &&
      listItem.innerText.toLowerCase().includes(value);

    if (target === true) {
      listItem.style.display = "block";
    } else {
      listItem.style.display = "none";
    }
  });
});
@import "https://cdn.jsdelivr.net/gh/KunalTanwar/normalize/css/normalize.inter.min.css";
body {
  height: 100%;
  display: grid;
  place-items: center;
}

.container {
  --space: 4px;
  --border-color: #e1e1e1;
  width: 100%;
  display: flex;
  max-width: 480px;
  flex-direction: column;
  row-gap: calc(var(--space) * 2);
}

.container input {
  border: 0;
  padding: calc(var(--space) * 4);
  box-shadow: inset 0 0 0 1px var(--border-color);
  transition: box-shadow 125ms ease;
}

.container input:focus {
  --border-color: #2e86c1;
}

.container ul {
  display: inherit;
  row-gap: var(--space);
  flex-direction: inherit;
}

.container ul .list-item {
  display: none;
  text-transform: capitalize;
  padding: calc(var(--space) * 4);
  box-shadow: inset 0 0 0 1px var(--border-color);
}
<div class="container">
  <input type="text" placeholder="Search for names" />
  <ul>
    <li class="list-item">adele</li>
    <li class="list-item">agnes</li>
    <li class="list-item">billy</li>
    <li class="list-item">bob</li>
    <li class="list-item">calvin</li>
    <li class="list-item">christina</li>
    <li class="list-item">cindy</li>
  </ul>
</div>

Now in my approach if your input is empty no list-item is visible.

Advertisement