Using Angular 4 (typescript), I have the below HTML code:
<div *ngIf="dataService.selected_markers.length == 0" id="tsnPasteContainer">
<form style="width: 100%; height: 100%">
<textarea id="tsn_list" style="width: 100%; height: 100%" type="text" name="tsn_list" placeholder="e.g. 2001311,2425302,2153542,2435974"></textarea>
</form>
</div>
I am trying to get the data that the user is typing into the textarea using:
public parseTSNs(){
let tsnString = document.getElementById("tsn_list").value;
console.log("User inputted string: " + tsnString);
}
This function is called by a button.
The code is not compiling due to:
Property 'value' does not exist on type 'HTMLElement'
This should be a simple function. What am I doing wrong? W3schools "getting values from textarea" shows '.value' as the required function!
The error "Property 'value' does not exist on type 'HTMLElement'" occurs when we try to access the value property on an element that has a type of HTMLElement . To solve the error, use a type assertion to type the element as HTMLInputElement before accessing the property.
The error "Property 'X' does not exist on type 'HTMLElement'" occurs when we try to access a property that doesn't exist on an element of type HTMLElement . To solve the error, use a type assertion to type the element correctly before accessing the property.
The HTMLInputElement interface provides special properties and methods for manipulating the options, layout, and presentation of <input> elements.
You just have to assert that the type of your element is HTMLTextAreaElement
. Because document.getElementById
returns HTMLElement
and not all html elements have the value
property:
let tsnString = (document.getElementById("tsn_list") as HTMLTextAreaElement).value;
But seeing as you use Angular, you should probably be using data binding instead of querying the values yourself
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With