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.

Cannot use two inputs with the same name

See original GitHub issue

Describe the bug I cannot use two inputs with the same name, that I switch between using a checkbox.

To Reproduce

const { handleSubmit, register, watch } = useForm();

const toggle = watch("toggle");

const onSubmit = data => {
  alert(JSON.stringify(data, null, 2));
};

return (
  <form className="App" onSubmit={handleSubmit(onSubmit)}>
    <input name="toggle" ref={register} type="checkbox" />
    {toggle ? (
      <div>
        <h1>Something here</h1>
        <input name="someValue" ref={register} />
      </div>
    ) : (
      <div>
        <input name="someValue" ref={register} />
        <h1>Something HERE</h1>
      </div>
    )}
    <button type="submit">submit</button>
  </form>
);

The <input name="someValue" /> gets unmounted and re-rendered - the new one doesn’t work. Only the original one works (when the checkbox is unchecked).

Codesandbox link (Required) https://codesandbox.io/s/friendly-chaum-w4tbj

Expected behavior I expect the second input to work just as the first one does.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
bluebill1049commented, Mar 15, 2020
Screen Shot 2020-03-15 at 5 41 18 pm

This is expected behavior, an alternative solution will be unregister the field manually.

useEffect(() => {
  unregister('test');
}, [toggle])

let me know if this solves your problem.

2reactions
morgs32commented, Apr 24, 2021

Or having a different input on mobile vs desktop and using css to hide one or the other…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it valid to have two input elements with the same name?
Yes, it is valid. This is Good <form name="form1"> <input type="hidden" name="url" value="1"> </form> <form name="form2"> <input ...
Read more >
Cannot map the same column to two inputs of a flowlet
I have tried to create similar Flowlet configuration and could find that by design we could not map same column to two inputs...
Read more >
How to Use Fields | Form Designer | User Guide | Epi Info
The field name cannot be changed after the data table is created. When testing a form in Enter, the data table is initially...
Read more >
Forms in HTML documents
Several checkboxes in a form may share the same control name. Thus, for example, checkboxes ... The INPUT element is used to create...
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