5

In typescript i need to implement specific caretPosition of an input element.But when i try to implement it shows createTextRange type does not exists in type HTMLElement. any one have solution for this?? Thanks in advance.

following is my code

private setCaretPosition() {
    var el = document.getElementById("ticketInfo");
    if (el !== null) {
        if (el.createTextRange) {
            var range = el.createTextRange();
            range.move('character', el.value.length);
            range.select();
            return true;
        }
        else {
            if (el.selectionStart || el.selectionStart === 0) {
                el.focus();
                el.setSelectionRange(length, length);
                return true;
            }
            else { // fail city, fortunately this never happens (as far as I've tested) :)
                el.focus();
                return false;
            }
        }
    }
}

it is showing createTextRange() undefined

2 Answers2

15

Cast el variable to HTMLInputElement.

UPD:

const input : HTMLInputElement = <HTMLInputElement>el;
sinedsem
  • 5,413
  • 7
  • 29
  • 46
  • When I try to cast I get a message saying HTMLElement is not assingable to type HTMLInputElement! How do I cast it so this works? – blueprintchris Apr 20 '18 at 09:26
0

It's strange because I actually don't see this method createTextRange in HTMLElement API

Where did you get this method, because where is no such method in API at all, maybe you using some external libs.

Andzej Maciusovic
  • 4,306
  • 1
  • 29
  • 40