So I've been trying to build an auto suggestion feature that will display multiple suggestions that have partial match or value within the inputbox
. It should also only start providing suggestions that starts with a specific character which is an @
Symbol
Example: Task 1 @james @john
Much like how facebook starts making suggestions when you provide the text with an @
Symbol
The code below listens to the values inserted in the inputbox
during a keypress. Then it stores those values in array where it will filter values that only contains an @
in the beginning. Then it makes a comparison with another array that has those values. Once a match is indicated it will then loop through the names-list
element for a match then adds a class called display
The code below is still a work in progress and that I'm still trying to figure it how to finish it with the proper syntax.
let listenOnInput = () => {
let handles = [];
let names = ['@john', '@jake'];
$(".task-label input").on("change keyup paste", function() {
let names = ($(this).val());
let splitHandles = names.split(' ');
$.each(splitHandles, function(i, value) {
if (splitHandles[i].indexOf('@') == 0) {
handles.push(splitHandles[i]);
}
for (let elements of handles) {}
})
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="task-label">
<input type="text">
</div>
<ul class="name-list">
<li class="name name-john">@John</li>
<li class="name name-jake">@John</li>
</ul>