question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Correct way to support multiple checkboxes with same name

See original GitHub issue

Describe the question?

What’s the ‘correct’ way to support multiple checkboxes, where the user can pick one or more values in the same group? E.g., pick zero or more colors.

To Reproduce

In pure HTML I’d write inputs with the same name:

    <input type="checkbox" name="colors" value="red">
    <input type="checkbox" name="colors" value="green">
    <input type="checkbox" name="colors" value="blue">

In react-hook-forms, how would I get the list of checked colors back as a list?

The obvious solution is to copy the above with the same name, but that doesn’t produce the correct value. However, this just produces the value of the first checkbox (checked or not).

// Doesn't work
rainbow.map(
    (c,i) => <label key={c}><input type="checkbox" value={c} name="sameName" ref={register} />{c}</label>
)

A working solution is to trick react-hook-forms into make the name an array, e.g., colors.0, which gives me an array of the checked checkboxes, but this feels like a hack.

// Produces the correct outcome
rainbow.map(
    (c,i) => <label key={c}><input type="checkbox" value={c} name={"withIndex."+i} ref={register} />{c}</label>
)

Codesandbox link https://codesandbox.io/s/react-hook-form-multiple-checkboxes-u95u2

Left-hand-side shows using the HTML style same name; clicking Submit just returns the value of the first checkbox.

Right-hand-side shows using a fake index, which appears to work correctly.

Additional context (in my real app, the list of values is very dynamic)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:36 (14 by maintainers)

github_iconTop GitHub Comments

23reactions
penxcommented, Aug 24, 2021

@bluebill1049 as above, I really think ‘how to handle checkboxes’ should be in the getting started or FAQ docs…

Please could you reopen this?

22reactions
noahdecococommented, Sep 28, 2021

Totally agree, there’s no mention of how to handle a group of checkboxes the “right” way anywhere in the documentation. I think it would be immensely useful to have an example in the getting started section.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Several Checkboxes sharing the same name - Stack Overflow
Several Checkboxes sharing the same name · 1. You must use the [] and then you can use in_array to determine which checkboxes...
Read more >
Multiple checkboxes with the same name are not handled ...
This was the first thing I tried with the Forms and I find the form is not show all the items. --- permalink:...
Read more >
Multiple checkboxes with same name but diff values
Hi, I have a problem evaluating a HTML Form like this: <form method="post" action="action.php"> <input type=checkbox name="del" value="1">.
Read more >
Should checkbox inputs have the same `name` attribute?
Now, I am aware of that for type=“radio” attribute, as only one radio button can be checked, but why should the same apply...
Read more >
<input type="checkbox"> - HTML: HyperText Markup Language
In this example you will see that we've given each checkbox the same name . If both checkboxes are checked and then the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found