0

I have the following problem: I have the code in the sandbox and I want to display a button if the checkbox is checked and the maxAmount is greater than 1. The maxAmount condition is not a problem but the checkboxes. I don't know how I can get the "checked"-value of a specific checkbox. I think I can not do anything with index, because the index-values are used multiple times when mapping the optionModules. Thanks in advance for your help. Sandbox-Link: https://codesandbox.io/s/nostalgic-germain-e18wh

devGuy
  • 113
  • 1
  • 9
  • Does this answer your question? [Get the value of checked checkbox?](https://stackoverflow.com/questions/11599666/get-the-value-of-checked-checkbox) – Lars Flieger Apr 19 '21 at 11:36
  • yes it helps to get the value of the specific checkbox. My problem is that I want to display a button behind each checkbox that is true. If I push the true/false-values in an array I don't know which value corresponds with a specific checkbox. – devGuy Apr 19 '21 at 12:43

1 Answers1

3

you could add a "onClick" to all of your checkboxes. Like this:

<input onClick={(x) => console.log(x.target.checked)} type="checkbox" value={option} 

And then you can push them into an array to filter whether your conditions are met.

Just like @Lars already mentioned look at this.

Carlotta
  • 334
  • 1
  • 13
  • Thanks for your answer. But how do I know which element in the array is the corresponding checkbox? – devGuy Apr 19 '21 at 12:37
  • you can push an object into an array like.. onClick={(x) => someArray.push({ name: option.name, isChecked: x.target.checked)} if this helps? – Carlotta Apr 19 '21 at 12:42
  • Thanks, I need to check it. But the problem is that there are objects that have the same option.name in different checkbox-groups. The sandbox is just a snippet from a more complex function. But I'll check it! – devGuy Apr 19 '21 at 12:47
  • You can add another field to the object, like "checkbox-group", then you can look for the group.name and the option.name :) – Carlotta Apr 19 '21 at 12:49