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
-
Does this answer your question? Get the value of checked checkbox?Lars Flieger– Lars Flieger2021-04-19 11:36:36 +00:00Commented Apr 19, 2021 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– devGuy2021-04-19 12:43:50 +00:00Commented Apr 19, 2021 at 12:43
Add a comment
|
1 Answer
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.
4 Comments
devGuy
Thanks for your answer. But how do I know which element in the array is the corresponding checkbox?
Carlotta
you can push an object into an array like.. onClick={(x) => someArray.push({ name: option.name, isChecked: x.target.checked)} if this helps?
devGuy
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!
Carlotta
You can add another field to the object, like "checkbox-group", then you can look for the group.name and the option.name :)