Nivitha G Nivitha G -3 years ago 267
TypeScript Question

How to add innerhtml in typescript?

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?

Answer Source

Html input elements have the value property, so it should be:

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

Notice that I also used HTMLInputElement instead of HTMLElement which does not have the value property.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download