The property doesn't exists in value type of HTMLElement
Asked Answered
N

2

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

Nole answered 12/11, 2015 at 6:29 Comment(4)
post your code, so we could better help you outKantos
hi i have updated the code.thanksNole
In el.setSelectionRange(length, length);, where is length defined or assigned a value?Grisham
hi Rob , that is just a global javascript value (i.e. 10, 20 or 30 what ever)Nole
B
15

Cast el variable to HTMLInputElement.

UPD:

const input : HTMLInputElement = <HTMLInputElement>el;
Brahmaputra answered 4/9, 2016 at 12:52 Comment(1)
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?Eckart
A
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.

Adjoint answered 19/11, 2015 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.