tags:

views:

141

answers:

1

I created an autosuggest script using php, ajax and some css but I can't figure out how to make it so when the script suggests results to allow the user to scroll down the list of results using keyboard arrows and then press enter to select desired result.

The script currently displays in a list format and and onclick the results the info is then filled into the search box.

What technique are people using to allow the user to scroll down with the keyboard and push enter to select a result choice?

Thanks

+1  A: 

I have been using jquery and one of its myriad of auto-complete plugins for this for a while... it's pretty much a no brainer to integrate it.. and you get a bunch of fairly complex niceties for free.

Here's a demo of one I've used a bunch: http://view.jquery.com/trunk/plugins/autocomplete/demo/

EDIT:

ultimately if you want to roll your own, you'll need to listen for keyboard events and keep track of your position in the list... then on enter key, push the current selected value into the text field. here is a good overview of keyboard event stuff:

http://unixpapa.com/js/key.html

http://www.javascriptkit.com/javatutors/javascriptkey.shtml

I would recommend using some library to abstract away all the browser nuttiness.. jquery is an awesome library with thousands of hours of testing and robust cross browser support. It normalizes the browser event model for you and really makes your code easier to read and maintain.

danb
the only feature that i feel is missing is the above feature, I enjoy making my own scripts plus I do not need alot of the extra frills
payling
that plugin allows keyboard navigation of the autocomplete list... or am i misunderstanding? type b in the first box in the demo and use the down arrow and enter... not what you mean?
danb
I hear you on the rolling your own thing.. but this is just such a solved problem... kinda boring :)
danb
if it were solved I would know the answer and wouldn't need your help =)
payling
I guess I'm still not understanding your problem... you just want to be able to move up and down the auto-complete list with the arrow keys and then pick the highlighted option with enter? or something else... as the plugin I pointed you at handles that use case.
danb
sometimes I like to know how things work behind the scenes too.. ya know?
payling
yes, exactly what the plugin does but I want to know how it does it so that I can implement it myself
payling
well... ultimately if you want to roll your own, you'll need to listen for keyboard events and keep track of your position in the list... then on enter key, push the current selected value into the text field. here is a good overview of keyboard event stuff http://unixpapa.com/js/key.html
danb
I'd really recommend using a library like jquery or prototype as your foundation as you will have to worry about significantly less cross browser issues. and write much less boilerplate
danb