How to add innerhtml in typescript?

Viewed 29686

I am using typescript in my application.

html code:

<input type="text" name="lastname" id="last">

Typescript code:

let myContainer = <HTMLElement>document.getElementById('last');
myContainer.innerHTML = "";

I want to set the empty value for the last name field using typescript. I am using above code. But cannot able to add empty value using typescript.

I also tried by using below code:

document.getElementById('last').innerHTML = "";

How to assign empty value for the textbox using typescript?

4 Answers

You can also use it like this

const element: HTMLElement = document.getElementById('personDetails') as HTMLElement
element.innerHTML = 'Hello World'

Works in Typescript 2.0

this is not a good practice, what we define in this way is not always the correct type.

let myContainer = document.getElementById('last') as HTMLInputElement;
myContainer.value = "";

or

const element: HTMLElement = document.getElementById('personDetails') as HTMLElement
element.innerHTML = 'Hello World'

if it is input use just HTMLInputElement

Related