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 l
Here is a rough method using the Selection
and Range
objects.
function getWord() {
var range = window.getSelection().getRangeAt(0);
if (range.collapsed) {
text = range.startContainer.textContent.substring(0, range.startOffset+1);
return text.split(/\b/g).pop();
}
return '';
}
You can see it in action here: http://jsfiddle.net/ggfFw/1/. This will not work in IE. If you need IE support look at the Rangy library.