5

I've made a simple implementation of an autocomplete which would call ajax and load a list of matches. Here's the JSfiddle

http://jsfiddle.net/83hJw/

This of course only shows an alert saying "OK" when the timer times out

You'll notice that it resets the timeout to give you a chance to finish entering what you want, ie if you pause for 0.3 seconds when typing, it will then run the search, so as not to request too many times to the server

I've also got it to only run if the number of characters are above 4 characters too

Put simply, I'm wanting to know if there is a better way of doing this in jQuery?

5 Answers 5

2

Try the jQuery UI autocomplete widget. It supports delay and minLength options out-of-the-box.

Sign up to request clarification or add additional context in comments.

Comments

1

I don't think that your implementation has anything wrong, but jQuery UI does have an autocomplete implementation as well. If you are already using jQuery UI, why reinvent the wheel.

2 Comments

Thanks Chad, As I've responded to Ali, this isn't really what I wanted advice on. And I'm not using jQuery UI either at present, and the extra weight of it for just this feature seems a bit much imo
@IAmThisGuy you can cherry-pick the parts of jQuery UI you want to use, and leave behind the parts you don't. jqueryui.com/download
1

Use jQuery UI's Autocomplete: http://jqueryui.com/demos/autocomplete/

1 Comment

It's not the autocomplete part that I'm wanting to do as such, it's the timeout part that is of interest to me. Thanks for the comment though, I've done some research into this before, but didn't find it met my needs
0

Personally I would say it's best to start showing examples from the first key press, and make sure the results shown are relative and sorted by popularity. This can help your users to find what they are looking for quickly and in fewer strokes.

6 Comments

Thanks sean, I'll take it under advisement. This is for a client site where he's wanting to search for products. The first two to three letters are likely to be of little benefit as he has a catalog of thousands of products hes wanting to type names in for quickly, and find them
Even more the reason to start from the first keypress. Look at Google, Bing and other various companies that use auto complete, they do it and their successful. I'd say follow by example.
I can see what you mean, but typing in the letter A in google, what are the chances they pick what you're looking for in those results?
With regards to the timeout I wouldn't do it every 0.3 second, I'd do it on the KeyUp event. This would prevent unwanted calls to the server.
Yes, the chance of A or any other letter returning a good answer on the first key stroke is minimal. But as I say, when you sort results by popularity, its a bit different, the chance are increased, along with it so is the usability.
|
0

If you don't want to migrate form JQuery to JQuery UI.

Try this:

https://github.com/devbridge/jQuery-Autocomplete

Note that just like JQuery-UI above supports "delay", but with "deferRequestBy" as name.

And supports "minLength", but with "minChars" as name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.