Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Set Caret to old position after updating value @textInputType #1679

@HeinrichKuhlmann

Description

@HeinrichKuhlmann

After the validation the caret got set to the end of the INPUT.
I fixed it for me by applying the following:

......
function elementHasCaret(element) {
return (
element.tagName == "INPUT" &&(
element.type == "text" ||
element.type == "email" ||
element.type == "password" ||
element.type== "number" ||
element.type== "date" ||
element.type== "datetime" ||
element.type== "datetime-local" ||
element.type== "month" ||
element.type== "tel" ||
element.type== "time" ||
element.type== "url" ||
element.type == "week"
)
|| element.tagName =="TEXTAREA"
)?true:false;
}

function getElementCaret(element) {
if(elementHasCaret(element))
return element.selectionStart;
else return false;
}

function setElementCaret(element,caret){
if(elementHasCaret(element) && caret != false){
element.setSelectionRange(caret,caret);
}
}

function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {

var listener = function() {
var caret = getElementCaret(element.get(0));
var value = trim(element.val());

if (ctrl.$viewValue !== value) {
  scope.$apply(function() {
    ctrl.$setViewValue(value);
  });
}
setElementCaret(element.get(0),caret);

};
....

Btw great Framework! Keep up the fantastic work.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions