0

I am trying to create an input field to search for text within an unordered list element. I have it working, but it only searches for the first word in the li.

How can I make it so it searches for all words?

        var input = document.getElementById("input");
        input.onkeyup = function () {
          var filter = input.value.toUpperCase();
          var lis = document.getElementsByTagName("li");
          for (var i = 0; i < lis.length; i++) {
            var name = lis[i].getElementsByClassName("name")[0].innerHTML;
            if (name.toUpperCase().indexOf(filter) == 0)
              lis[i].style.display = "list-item";
            else lis[i].style.display = "none";
          }
        };
<input type="text" id="input" placeholder="Search for something..." />

<ul>
<li><h3 class="name">Walking on the moon</h3></li>
<li><h3 class="name">Visiting the zoo</h3></li>
<li><h3 class="name">Freshly baked bread</h3></li>
</ul>

For example, how can I search for moon, zoo, and bread, and have those li's show when searched for?

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
javinladish
  • 907
  • 1
  • 9
  • 18

0 Answers0