I am listening for selectionStart and selectionEnd with knockout and it's working fine when the user clicks with the mouse and drags rightward to create the selection. But if the user clicks in the text and drags the mouse leftwards to create the selection, the proper values are not being returned: selectionEnd and selectionStart are in that case the same value, e.g. 6 and 6 when I select a six-letter word moving right-to-left with the mouse.
Here's my binding:
<input type="text" data-bind="textInput: Model.searchWord, event:{keyup: Model.saveSelection, mouseup: Model.saveSelection, mousedown: Model.saveSelection, click: Model.saveSelection}" />
P.S. I've also tried without listening for the click event.
and here's the code:
self.selectionStart = ko.observable();
self.selectionEnd = ko.observable();
self.saveSelection = function (v1, keyEvent) {
self.selectionStart(keyEvent.target.selectionStart);
if (keyEvent.target.selectionEnd) {
self.selectionEnd(keyEvent.target.selectionEnd);
} else {
self.selectionEnd(0);
}
if (keyEvent.type == "mousedown") return true;
if (keyEvent.type == "mouseup") return true;
}