javascript - Navigate to next text element with keyboard arrow when at start or end of text -
i having hard time navigating 1 element using keyboard arrow; want navigation occur when cursor @ start or end of text i.e. "move right if @ end of text , move left @ start of text, press left arrow when @ end of text not want navigate, want let default event happen."
here's start of want, try1
this function need writing
function isendorstartoftext($this) { return true; // @todo calculate here }
you want separate 2 checks because can @ end of string , press left, shouldn't focus previous field.
in modern browsers can node.selectionstart on node, check on left-key press if selection start 0:
if ($this[0].selectionstart === 0) { e.preventdefault(); if ($('#'+previous_id).length > 0) { $('#'+previous_id).focus(); } }
for right key, check:
if ($this[0].selectionstart === $this.val().length) { e.preventdefault(); if ($('#'+next_id).length > 0) { $('#'+next_id).focus(); } }
i updated example: http://jsfiddle.net/88vl8/7/ (tested in chrome)
ps: code not work on ie, there's polyfill available here on stackoverflow, search "how caret position in textarea?"
Comments
Post a Comment