Where el
is a reference to an input or textarea.
function moveCursorToEnd(el) {
if (typeof el.selectionStart == "number") {
el.selectionStart = el.selectionEnd = el.value.length;
} else if (typeof el.createTextRange != "undefined") {
el.focus();
var range = el.createTextRange();
range.collapse(false);
range.select();
}
}
I hope this solves the caret problem if I use <span contenteditable=”true”</span> instead of an <input type=”text”> :)
var tmpStr = field.val();
field.val(”);
field.val(tmpStr);
It doesn’t work on elements with contenteditable=”true”
Chromium 75.0.3770.100
input tag with type=”number”
input.selectionStart is null
input.createTextRange is undefined
Thanks a lot
This version was useful to me (rtl problem):
const handle_phone_click = (el) => {
if (el.target.selectionStart == 0) {
el.target.selectionStart = el.target.selectionEnd = el.target.value.length;
}
}