12

How do I enable exact match from start of string using jquery autocomplete with input from simple array?

If i have the following in an array:

  • smart
  • oversmart
  • smartland
  • undersmart
  • verysmart

And if I am typing "sma..." in the text input, i must be shown only smart and smartland, not the others.

VKVK
  • 131
  • 1
  • 1
  • 5
  • possible duplicate of [jQuery Autocomplete plug-in search configuration](http://stackoverflow.com/questions/2382497/jquery-autocomplete-plug-in-search-configuration) – j08691 May 05 '12 at 17:18

5 Answers5

16

You just need to modify source parameter as a function to suit your needs. Like this:

http://jsfiddle.net/UKgD6/


Update: Adding code to answer:

var acList = ['smart', 'oversmart', 'smartland', 'undersmart', 'verysmart'];
$('#ac').autocomplete({
    source: function (request, response) {
        var matches = $.map(acList, function (acItem) {
            if (acItem.toUpperCase().indexOf(request.term.toUpperCase()) === 0) {
                return acItem;
            }
        });
        response(matches);
    }
});
Mottie
  • 84,355
  • 30
  • 126
  • 241
keune
  • 5,779
  • 4
  • 34
  • 50
3

The way to do this is documented at http://api.jqueryui.com/autocomplete/

<script>
    var tags = [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ];
    $( "#autocomplete" ).autocomplete({
      source: function( request, response ) {
              var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
              response( $.grep( tags, function( item ){
                  return matcher.test( item );
              }) );
          }
    });
</script>
Jonathan Vance
  • 434
  • 5
  • 22
1

You can use regular expressions to match the entered substring with the values in the array you have.

atmd
  • 7,430
  • 2
  • 33
  • 64
0

JQuery has way too many plug-ins. Just sprinkle a little regEx on it and write something like this (I didn't test it but I've done similar stuff before):

$('#someTextInput').keyup( function(){
    var regExMatch = new RegEx( '^' + $(this).val() );
    $_LIs = $('#someUl li');
    $_LIs.hide();
    $_LIs.each( function(){
        if( this.innerText.match(regExMatch) ) { $(this).show(); }
    } );
} );

You can remove the '^' + in the new RegEx parameters to restore the behavior you're describing as problematic. '^' signifies beginning of a line/string selection so 's' won't match unless it's at the start.

Erik Reppen
  • 4,605
  • 1
  • 22
  • 26
  • Erik - thank you, i will keep this in mind and tweak to see how it fits inside an autcomplete... – VKVK May 06 '12 at 02:20
0

This has changed since the previous answer was accepted. There is now a lookupFilter option to do this and it is much simpler. From https://stackoverflow.com/a/23239873/1204434, just add this to your list of options:

lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
                return suggestion.value.toLowerCase().indexOf(queryLowerCase) === 0;
            },
Community
  • 1
  • 1
felwithe
  • 2,683
  • 2
  • 23
  • 38