Error "Property 'value' does not exist on type 'HTMLElement'" in TypeScript
Hello everyone! I just started learning TypeScript and ran into a problem when working with DOM elements. I have a simple code that gets the value of an input field on button click:
```typescript
const button = document.getElementById('myButton');
const input = document.getElementById('myInput');
button.addEventListener('click', () => {
const inputValue = input.value; // Error!
console.log(inputValue);
});
```
But TypeScript throws an error: "The property 'value' does not exist on value of type 'HTMLElement'". I understand that HTMLElement is the base type for all HTML elements, but why can't I access the value property? After all, input does have value! I tried to explicitly specify the type, but nothing helps. Please advise how to properly solve this problem? Maybe I need to
No answers yet.
Log in to leave an answer