Valerie
Valerie

Reputation: 41

Sortable/Searchable List with Javascript

So I'm using this how to from w3schools but I want to search an entire <li> rather than just the <a> tags. I can see that this is the problem a = li[i].getElementsByTagName("a")[0]; but I can't figure out how to change it?

Here's their markup:

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

But I want this:

    <input type="text" id="myInput" onkeyup="myFunction()" 
    placeholder="Search for names..">

    <ul id="myUL">
       <li><a href="#">Adele</a> - Some more text that gets picked up by 
       search.</li>
       <li><a href="#">Agnes</a> - Some more text that gets picked up by 
       search.</li>
    </ul>

And here's the script:

    <script>
    function myFunction() {
    // Declare variables
    var input, filter, ul, li, a, i;
    input = document.getElementById('myInput');
    filter = input.value.toUpperCase();
    ul = document.getElementById("myUL");
    li = ul.getElementsByTagName('li');

    // Loop through all list items, and hide those who don't match the         
    search query
    for (i = 0; i < li.length; i++) {
            a = li[i].getElementsByTagName("a")[0];
            if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
                li[i].style.display = "";
            } else {
                li[i].style.display = "none";
            }
        }
    }
    </script>

Upvotes: 0

Views: 106

Answers (1)

Fady Sadek
Fady Sadek

Reputation: 1160

You just have to replave the a tag in the indexof line by the following

if (li[i].innerHTML.toUpperCase().indexOf(filter) > -1) {

You can have a look at the working example here but i have not changed any styling i just changed where it searches

function myFunction() {
    var input, filter, ul, li, a, i;
    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];
        if (li[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
            li[i].style.display = "";
        } 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 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.header {
  background-color: #e2e2e2;
  cursor: default;
}

#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="#" class="header">A</a></li>
  <li><a href="#">Adele</a>some other funcky text</li>
  <li><a href="#">Agnes</a></li>

  <li><a href="#" class="header">B</a></li>
  <li><a href="#">Billy</a></li>
  <li><a href="#">Bob</a></li>

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

Upvotes: 2

Related Questions