I am new to react. I want to get value of input with type number.
var x = //now I want to get value of an input
Could you help me with it please?
I am new to react. I want to get value of input with type number.
var x = //now I want to get value of an input
Could you help me with it please?
You can find the help by using the below code and you can run it also to get the value of event.
function App() {
const handleOnChange = (e) => {
console.log(e.target.value);
}
return (
<div className="App">
<input name='myInput' type={"text"} onChange={(e) => handleOnChange(e)} />
</div>
);
}
This can be don't with multiple ways.
You can set the onChange or onInput event on the input element to get the value of the input whenever the user writes on the input element. Ex:
<input onChange={(event) => console.log(+event.target.value) }
Also, you can have a template reference from the input, you can use it whenever you need to get the input in a specific condition, let's say add an event listener depending on a condition or get its value depending on another element event.
For example, ( password and confirm password inputs ).
Code Ex:
import { useRef } from 'react'
const inputRef = useRef()
<input ref={inputRef} />
And whenever you want to get the input value
inputRef.currunt.value