Home > OS >  javascript nested ul li > ul li search
javascript nested ul li > ul li search

Time:09-21

I got a code like below from w3schools. but there are nested ul's in my list. the code here does not work on it. How do I adapt this code to nested ul li?

w3schools code:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</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>

my list:

<ul id="myUL">
    <li>
        <a href="#">Adele</a>
        <ul>
            <li>
                <a href="#">Mariam</a>
                <ul>
                    <li>
                        <a href="#">Andreas</a>
                    </li>
                    <li>
                        <a href="#">Dimitru</a>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
    <li><a href="#">Agnes</a></li>
    <li><a href="#">Billy</a></li>
    <li>
      <a href="#">Bob</a>
      <ul>
          <li>
              <a href="#">Alexander</a>
          </li>
      </ul>
    </li>
    <li><a href="#">Calvin</a></li>
    <li><a href="#">Christina</a></li>
    <li><a href="#">Cindy</a></li>
</ul>

As you can see I have multiple ul - li nested in my list. so the search system is not working. how do i run it? You write a name on the entry. Filters if the name you type is in the list. but nested ul -li are not filtered out. I want nested ul -li to be filtered as well.

CodePudding user response:

You can't use display:none property, becuase it hides the li and it's child li's you may use visibility instead, Check the following:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</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>
        <ul>
            <li>
                <a href="#">Mariam</a>
                <ul>
                    <li>
                        <a href="#">Andreas</a>
                    </li>
                    <li>
                        <a href="#">Dimitru</a>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
    <li><a href="#">Agnes</a></li>
    <li><a href="#">Billy</a></li>
    <li>
      <a href="#">Bob</a>
      <ul>
          <li>
              <a href="#">Alexander</a>
          </li>
      </ul>
    </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 = document.querySelectorAll('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.visibility = "visible";
        } else {
            li[i].style.visibility = "hidden";
        }
    }

}
</script>

</body>
</html>
  • Related