Search code examples
reactjsformsreactstrap

Can't take value from Input Type ="select"


I am trying to take value from a drop down menu but isn't able to do so . My code for the dropdrop is :

  <Input type="select" name="type" id="etype" value={this.state.type} onChange={this.changeTypeHandler}>
                            <option value="Doctor">Doctor</option>
                            <option value ="Nurse"> Nurse</option>
                        </Input>

while my changeTypeHandler is

 changeTypeHandler = (event) => {
        this.setState({ type: event.target.value });
    }

I have seen this.state.selectedValue in on of the solution for type . So what should be done in this case ? Sorry If its a basic question but I am new to react and isn't able to find the solution for this .Thanks


Solution

  • Try it like this :

    <select name="type" id="etype" value={this.state.selectedValue} 
          onChange={this.changeTypeHandler}>
          <option value="Doctor">Doctor</option>
          <option value ="Nurse"> Nurse</option>
    </select >
    

    and for onChange function

    changeTypeHandler =(event) =>
    {
        this.setState({selectedValue:event.target.value});
    }