Javascript - Position Caret

DOM

How can we position the caret?

var el = cmp.el.dom;
if (el.createTextRange) {
    var range = el.createTextRange();
    var pos = el.value.length;
    range.moveStart("character", pos);
    range.select();
} else if (el.setSelectionRange) {
    el.setSelectionRange(el.value.length,0)
}

The above code works for IE11 (with compatibility view setting on). I used the above code position the caret at the end of an input field. See https://forum.sencha.com/forum/showthread.php?95486-Cursor-Position-in-TextField

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License