Skip to content
Advertisement

JavaScript get word before cursor

Okay, I’ve been looking all over the web to find a solution but I couldn’t find one, is there a way to get the word before the caret position in an editable div so a bit like:

JavaScript

This should return the word “some”… I don’t know if this is possible, I would be glad for any help, thanks :).

Advertisement

Answer

With using Caret Position finder method provided here this will do what you want.

JavaScript
JavaScript

Here is also a jsfiddle.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement