I have this simple form and I am having a hard time in changing and getting the state of this one array.
The state is set
this.state = {
.....
treatment: [],
.....
}
On change
onChange= (e) => {
this.setState({
[e.target.name]: e.target.value,
error: ''
})
}
The array element is called on click in form with one array element in it Click Here to add Treatment
On clicking the button the JS kicks in
$(document).ready(function (){
$('#addHidden').click(function(){
var addtext = $('.hidden_box').html();
$('#addHiddenBox').append(addtext);
});
$("#addHiddenBox").on("click", ".removeText", function () {
$(this).closest(".boxAdded").remove();
});
});
and brings in this hidden element
<div className="hide hidden_box">
<div className="treatments col-sm-6 boxAdded">
<div className="form-group">
<input className="form-control" type="text" placeholder="Treatment" name="treatment[]" value={this.state.treatment} onChange={this.onChange}/>
<button className="btn btn-danger removeText" type="button"><i className="fa fa-times" aria-hidden="true"></i></button>
</div>
</div>
</div>
Iam not able to set the state of treatment[] here. The states are coming blank.
e.target.nameande.target.valueis whenonChangeis triggered. Maybe use console.log debuggingtreatmentand nottreatment[], and then try below debugging. ``` onChange= (e) => { console.log('name', e.target.name); console.log('value', e.target.value); this.setState({ [e.target.name]: e.target.value, error: '' }) } ```