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.

<input> tags within radio buttons do not change checked attribute when switching between choices

See original GitHub issue

When changing radio buttons in the UI, the checked attribute on the <input> tag does not get updated. Upon inspection of the DOM, you can see that the checked attribute remains on the first child regardless if it’s checked or not.

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

When selecting an option on a material-ui radio group, the checked attribute that exists within the <input> tags does not update accordingly. It will remain on the top level child even if it’s no longer selected.

Expected Behavior 🤔

The checked attribute on the <input> should appear on newly selected items and be removed from the one no longer selected. Or the checked attribute should be showing a boolean value to determine if the item is selected or not.

Steps to Reproduce 🕹

Steps: Navigate to: https://material-ui.com/components/radio-buttons/

  1. Go to the first radio group under the section RadioGroup
  2. Inspect element on the option Female (if this is the first radio group collection)
  3. Locate the checked attribute within the <input> element in the DOM selector
  4. Within the UI, click on any other selection within that radio group
  5. Within the inspector, the checked attribute on the previous selection will remain
  6. Inspect the second element which was the new selection
  7. Within the inspector, the second element will not contain the checked attribute

Context 🔦

Our automation on our team requires some way to tell within the DOM that an item is selected. Our current workaround is to attach an aria-selected to the element. However, it would be nice if the checked attribute would either update with a boolean, or remove itself from the unselected element and attach itself to the newly selected element.

Your Environment 🌎

Tech Version
Material-UI v4.9.0
React ^16.9.0
Browser Chrome
TypeScript
etc.

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
xylish7commented, May 10, 2022

@dwjohnston My current issue is related to using RadioGroup. I’ve created this codesandbox to actually test the issue I’m having: https://codesandbox.io/s/gifted-sutherland-4mw0w6.

So in the codesandbox I tried to implement a checked={isChecked} thing as you mentioned, but doing so on the Radio components will still not work.

At first, since Location 2 is selected by default, if you inspect Location 2 element , indeed it will have an attribute of checked="":

image

Now, because of line 38 from the codesandbox (checked={location === selectedLocation}), I expect when clicking, for example, on Location 3, that the checked attribute should be present on Location 3 input and not Location 2. However it is still present on the initial selected radio button.

image

Is this the desired result ? And if it is, what should be the workaround ? Thank you and sorry for the late response!

1reaction
dwjohnstoncommented, Jul 22, 2022

@xylish7 Looks like you’re right, the checked = technique won’t work. This appears to be standard behaviour in React, not something to do with MUI itself (here’s a plain example: https://codesandbox.io/s/fast-worker-m4kxr1?file=/src/App.js)

It would be nice to have a authorative reference for this, but to hazard an explanation, I imagine it’s because ‘checked’ is one of those properties that ‘only apply when the element first mounts’ that they intentionally defined this this behaviour in React. They probably do the same for properties like defaultValue too.

My apologies for setting you down the wrong path.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Radio Buttons "Checked" Attribute Not Working - Stack Overflow
It seems that you are setting the attribute 'Checked' on all #radio-elements in your first example. – etoxin. Jan 9, 2013 at 3:58....
Read more >
<input type="radio"> - HTML: HyperText Markup Language
The value attribute is a string containing the radio button's value. The value is never shown to the user by their user agent....
Read more >
Exposing Additional Form Fields via Checked Radio Buttons
Disabling the input when hiding it will block submission of any value set before it was hidden, even if it's required when visible....
Read more >
lightning-input - documentation - Salesforce Developers
Use lightning-radio-group instead of input type radio for radio buttons. ... when the value of the component is changed, use the onchange attribute....
Read more >
Radio buttons | U.S. Web Design System (USWDS)
Since only one radio button can be selected at a time (within the same group), each available choice must be its own item...
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