Understanding jQuery has always been a challenge for me. I find myself spending hours reading documentation every time I try to implement it, only to achieve very little progress. Here's the code I'm currently working on with live-editing available:
http://codepen.io/anon/pen/vKFBy
The code filters records in real-time as you type in an input field. However, I'm facing an issue with the search method it uses. Let me explain further:
<ul id="list">
<li class="in"><span>google.com</span></li>
<li class="in"><span>gmail.com</span></li>
<li class="in"><span>samsung.com</span></li>
<li class="in"><span>amazon.de</span></li>
<li class="in"><span>apple.de</span></li>
</ul>
For example, when I type "G" or "g," amazon.de and apple.de disappear because they do not contain the letter "G," while google.com, gmail.com, and samsung.com remain visible. This is the issue I'm facing. I do not want to keep samsung.com just because it contains a "G" at some point. I want to only display elements where the span begins with a specific string. Essentially, this is what I'm trying to achieve:
When I type "G":
<ul id="list">
<li class="in"><span>google.com</span></li>
<li class="in"><span>gmail.com</span></li>
<li class="hiding out hidden"><span>samsung.com</span></li>
<li class="hiding out hidden"><span>amazon.de</span></li>
<li class="hiding out hidden"><span>apple.de</span></li>
</ul>
Continuing to type "Go":
<ul id="list">
<li class="in"><span>google.com</span></li>
<li class="hiding out hidden"><span>gmail.com</span></li>
<li class="hiding out hidden"><span>samsung.com</span></li>
<li class="hiding out hidden"><span>amazon.de</span></li>
<li class="hiding out hidden"><span>apple.de</span></li>
</ul>
I am aware of the syntax I should be using, such as:
indexOf(searchTerm)
Or:
[span^="'+searchTerm+'"]
Or:
str.match(/^searchTerm/)
However, I am struggling to incorporate these into the current if statements without breaking the script every time.