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

2
  • Does this answer your question? Get the value of checked checkbox? Commented 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. Commented Apr 19, 2021 at 12:43

1 Answer 1

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.

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

4 Comments

Thanks for your answer. But how do I know which element in the array is the corresponding checkbox?
you can push an object into an array like.. onClick={(x) => someArray.push({ name: option.name, isChecked: x.target.checked)} if this helps?
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!
You can add another field to the object, like "checkbox-group", then you can look for the group.name and the option.name :)

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.