Reputation: 25
I have a function as below in one of my old application its working fine in Chrome but not working fine in IE 11.I tried a solution "document.getSelection()",but it does mot support moveStart method.Can anybody help me with rewriting the code?
function getCaretPos(element) {
var oSel = document.selection.createRange();
oSel.moveStart('character', -element.value.length);
return oSel.text.length;
}
Upvotes: 2
Views: 1042
Reputation: 3458
For elements textarea
and input
you can get caret position using their selectionStart
and/or selectionEnd
properties.
Unfortunately IE11 seems to don't support selectionDirection
property, so if it matters, you'll have to add another listener to find out selection.
Demo:
textarea.addEventListener('mouseup', function(){
console.log("textarea",textarea.selectionStart, textarea.selectionEnd)
})
input.addEventListener('mouseup', function(){
console.log("input", input.selectionStart, input.selectionEnd)
})
<textarea id="textarea">some text</textarea>
<br>
<input id="input" value="some text"/>
Upvotes: 1