Search code examples
javascriptsearchautocompleteautosuggest

Search suggestion cache javascript


I am having a Search text-box for which I have to implement a search suggestion(like google search) kind of feature.

for example, if I type "abc" in the search box, I will get suggestions like
abcdef
xyzabcxyz

here each of these unique suggestion represents a jsp page whose url can be "hard-coded" in a java-script array. so when I click any suggested value, the webpage at the corresponding link is displayed.

For this, can you suggest me any efficient methods of implementing this.
This must be in pure javascript, no third-party libs like jquery,etc., can be used

Thanks in advance.


Solution

  • Assuming your data is in an associative JS array similar to this

    var locationArray = {
        'searchValue1' : 'url1',
        'searchValue2' : 'url2',
            ...
    };
    

    and your search input field has a function associated to onkeyup

    <input type="text" onkeyup="UpdateSuggestions(this);">
    

    then the rest is simple JavaScript string functions.