Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a typed value from an Input?

Tags:

reactstrap

I would like to get a typed value from a ReactStrap Input component, via the onChange function. The aim is to use a text input only but be able to get a specific typed value (String or Number in my example below).

<Input valueAs="String" name="input1" type="text" onChange={this.onChange}></Input>
<Input valueAs="Number" name="input2" type="text" onChange={this.onChange}></Input>

You can see the expected type is defined by the valueAs attribute. I expect to get an event in this.onChange with:

  • a value as a String for Input 1
  • a value as a Number for Input 2

What is the best way to do this with React / Reactstrap?

like image 852
Sophie Avatar asked Dec 12 '17 21:12

Sophie


People also ask

How do you find the text value of an element?

Use the textContent property to get the text of an html element, e.g. const text = box. textContent . The textContent property returns the text content of the element and its descendants. If the element is empty, an empty string is returned.

What is input type text value?

The Input Text value property in HTML DOM is used to set or return the value of a value attribute of the input field. The value attribute specifies the initial value of the input Text Field. It contains the default value or the user types.


1 Answers

Input component of reactstrap does not have a property called valueAs. To get value in a format you need, you can do:

<Input name="input1" type="text" onChange={(e) => this.onChange(`${e.target.value}`)}/>
{/* to make it integer you can add unary plus sign like so: */}
{/* this.onChange(+e.target.value) */}
like image 177
iamawebgeek Avatar answered Nov 10 '22 21:11

iamawebgeek