How to select multiple checkboxes in Reactjs
P粉950128819
P粉950128819 2024-04-06 18:55:21
0
1
497

I have an option list in which I want to select multiple checkboxes but am unable to do so, what is wrong with this code?

Sandbox URL

P粉950128819
P粉950128819

reply all(1)
P粉545218185

According to your requirements, you can change handleCheckboxQuestionInput to:

const handleCheckboxQuestionInput = (e) => {
    const answer = e.target.value;
    setSelectedAnswers((prevAnswers) => {
      if(prevAnswers.includes(answer)) {
        return prevAnswers.filter(e => e !== answer)
      } else {
        return [...prevAnswers, answer]
      }
    });
  };

and change at the input element to:

 handleCheckboxQuestionInput(e)}
 />

Hope it helps you

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template