I am using selectize.js. Currently it looks like this:
It shows not only words that start with 'arm', but also words (or options) that contain 'arm' as a substring somewhere else.
I would like to force the function to show only those words (or options) that start with 'arm'.
I checked the usage documentation at https://github.com/selectize/selectize.js/blob/master/docs/usage.md but was not able to figure out how to solve this.
Does anybody have any ideas?
You can use the
score
property which loops through the entire list and sorts items. 1 being the most relevant one, 0 is considered as not matching, and the item is excluded. Knowing this we can write our own function :)The score function is called each time a new character is entered. The inner function of score then checks each item, here's the structure of an item.
Knowing this we take
item.text
, make all letters lowercase (remove.toLowerCase()
if you don't want this) and compare it with the valuesearch
(also lowercase). Whenitem.text
starts with the value insearch
, then return 1 - item should be included - else 0 and the item is excluded from the list. Here's the entire function forscore
.Below is a working example to see it in action.