How to detect and select the last written word when TAB is pressed in a textarea
or contenteditable div
?
I started to do it with a buffer of the last characters written or key-presses, but then I noticed that there are multiple corner cases:
let's say the user writes HELLUBACKSPACEO
the user moves with arrow keys
the user can separate words with SPACE (that's what I was using) but also ENTER or a comma, etc.
Is there a nice way to detect the last written word?
Goal: to code an autocomplete feature, example: thx + <TAB>
=> Text is replaced by "Thank you very much."
The sky blue
, then I move the cursor aftersky
and I typeis
. Then I pressTAB
: the detected word should beis
. It's not the last word in the textarea (which isblue
), but it's the last written word... Another way to say it is that it's the last word before cursor, it would work too. – Basj Jun 11 '18 at 9:36