V
V
vertically-challenged2022-02-16 14:40:32
typescript
vertically-challenged, 2022-02-16 14:40:32

How to get value from Input on form submission using TypeScript?

Usually I did this with JS, but it doesn’t work with TS, an error is displayed :

Element implicitly has an 'any' type because expression of type '0' can't be used to index type 'EventTarget'.
Property '0' does not exist on type 'EventTarget'


This is how I'm trying to get value :
<form onSubmit={
      (event) => {
        event.preventDefault()
        console.log(event.target[0].value)
      }
    }>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mesuti, 2022-02-16
@vertically-challenged

The value of input is in state.
Take it from there.
If there would be a normal layout, then console.log(event.target['test'].value)(this example is from Vadim )
And you use react, so all values ​​are taken from states, layout is only for rendering

<form onSubmit={
    (event) => {
        event.preventDefault()
        console.log(inputCityValue)
    }
}>
    <Input value={inputCityValue} onChange={onChangeHandlerWrapper(setInputCityValue)}/>
    <button type='submit'><span>Обновить данные</span></button>
</form>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question