Search code examples
javascriptgoogle-chromesafaridom-eventsevent-bubbling

Google Chrome cancel up and down arrow events


I'm making an AutoSuggest widget for a website, which works in the way that when an user writes something in input text box the div with suggestions is displayed and an user can navigate through it by mouse or by up and down arrows. Each word is suggested separately (not like in Google suggest where it looks on the whole phrase.

I have a problem with Google Chrome input box as when I'm pressing up or down arrow there is a default behaviour of browser - jump with carret to the end or beginning of the text box (like with Home or End buttons). There is no such effect on Firefox or Internet Explorer. How could I disable this effect?

I'm returning 'false' from the event handler function and also used a function from here http://www.javascripter.net/faq/canceleventbubbling.htm but still carret is jumping on Chrome...

Edit: same effect on Safari...


Solution

  • Not exatly a true solution for a question but I've done a workaround by using this functions http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea/ and catching the carret position before event (or not exatly before as when keydown event is fired the carret is still on good position) and then setting it back after event.