I was able to resolve the issue by adjusting my search filter algorithm. Now, I can search regardless of word order, but the results are not as specific as I need them to be. When I type "Correy" in the search bar, it shows all results containing that word correctly. However, when I try to search for a specific "Correy Adele", it still displays all "Corey" results. This is not what I want. Any suggestions on how to improve this?
function updateResults() {
var keywords = $('input').val().toUpperCase().split(' ');
var listItems = $('li');
var link;
var ul;
var textValue;
ul = document.getElementById("myUL");
for (var i = 0; i < listItems.length; i++) {
link = listItems[i].getElementsByTagName("a")[0];
textValue = link.textContent || link.innerText;
for(var j = 0; j < keywords.length; j++) {
if (textValue.toUpperCase().indexOf(keywords[j]) > -1 ) {
listItems[i].style.display = '';
// no need for further matches
} else {
listItems[i].style.display = 'none';
}
break;
}
}
}
<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>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="myInput" onkeyup="updateResults()" placeholder="Search for names.." title="Type in a name">
<ul id="myUL">
<li><a href="#">Adele Correy</a></li>
<li><a href="#">Agnes Green</a></li>
<li><a href="#">Billy Correy</a></li>
<li><a href="#">Bob Green</a></li>
<li><a href="#">Calvin Green</a></li>
<li><a href="#">Christina Correy</a></li>
<li><a href="#">Cindy Correy</a></li>
</ul>