1

Hi i am trying to manage multiple checkbox with multiple input in same map can anyone tell me how can i do it here is my code the main problem is that when i try to add some input afterwards another ita takes wronge indexing

GetCryptoHtml = (criptoList) => {
return criptoList.map((value, i) => {
  return (
    <>
      <div className="col-md-3 col-12">
        <input
          className="checked"
          id={i + "checkbox"}
          type="checkbox"
          onChange={(e) => this.onChangeCheck(e, value, i)}
          name={"cryptoCurrency" + [i]}
          value={value.id}
        />{" "}
        <label for="vehicle1"> {value.name}</label>
      </div>
      <div className="col-md-9 col-12">
        <div className="form-group">
          {/* {
          checkingInput ? */}
          <input
            type="text"
            id={i + "percentage"}
            name={"percentage" + [i]}
            onChange={(e) => this.onChangeInputBox(e, i ,value.name)}
            value={this.state.percentage[i]}
            className="form-control"
          />
          {/* <input type="text" onChange={(e) => this.checkWordInput(e)} id="percentage[i]" name="percentage[i]" /> : <></> */}
          {/* } */}
        </div>
      </div>
      <br />
    </>
  );
});

And Here's the onChange function for both

  onChangeCheck = async (e, value, i) => {
var percentage = document.getElementById(i + "percentage").value;
if (e.target.checked) {
    this.setState({
        checkedCheck: true
    });
    this.state.data2.push(value.short_name);
    if (this.state.percentage[i] !== undefined) {
        this.state.percentage[i] = this.state.percentage[i];
    } else {
        this.state.percentage.push("");
    }
} else {
    this.setState({
        checkedCheck: false
    });
    this.state.data2.pop(value.short_name);
    this.state.percentage[i] = "";
}

this.setState({
    cryptoCurrency: "update"
});

1 Answer 1

1

Maybe the issue you are facing is because of the indexing process from where i can see the problem is the your index value on change is setting by default 0 and everytime you add your input it takes one input back to add value in current section. You can check the indexing and current id if they both are matched and after then enter the value using event

Sign up to request clarification or add additional context in comments.

4 Comments

I've tried using id indexing but every time I am adding new field the first value disappears
It seems every time you add using your id you first have to check if your current array contain any source of data in state and after that you can add the existing data with your new array
Or you can use Jquery to add your value on current value by using dynamic id parameter
Okay i can try using jquery. Thank you @Hacker Boy 404

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.