'event.target.value' are 'undefined' of 'onChange(event)' for 'Checkbox', 'Radio', and 'Dropdown'
See original GitHub issueDetails
Version: "semantic-ui-react": "^0.54.2"
To reproduce the issue, here is the onChange() function:
function onChange(event) {
console.log(`event.target.value: ${JSON.stringify(event.target.value)}`);
}
and use the following JSX for the testing:
const App = () => (
<Container>
<Divider hidden />
<Header as='h1'>Checkbox</Header>
<Segment>
<Checkbox toggle label="Toggle" onChange={onChange}/>
<Divider hidden />
<Radio label="Radio" onChange={onChange} />
<Divider hidden />
<Input type="text" onChange={onChange} />
<Divider hidden />
<Dropdown selection options={options} placeholder='Dropdown' onChange={onChange} />
</Segment>
</Container>
)
Here is the sample code link for the test.
http://codepen.io/twang/pen/dpdowZ
Just open the Developer Tools, and check the Console, and then change each components’ value.
For the <Input>, the event.target.value returns correct value of the <input ...>, however, for the <Checkbox>, <Radio> and <Dropdown>, the event.target.value are undefined.
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (8 by maintainers)
Top Results From Across the Web
event.target is undefined on a onChange in radio button
Once I put the react fragment around the inputs the code works? e.currentTarget.value is defined. – evolutionxbox. Jul 8, 2021 at 13:44.
Read more >.val() | jQuery API Documentation
The .val() method is primarily used to get the values of form elements such as input , select and textarea . When called...
Read more >onchange Event - W3Schools
The onchange event occurs when the value of an element has been changed. For radiobuttons and checkboxes, the onchange event occurs when the...
Read more >useForm - register - Simple React forms validation
Performant, flexible and extensible forms with easy-to-use validation.
Read more >user-event v13 - Testing Library
import userEvent from '@testing-library/user-event' test('double click', () => { const onChange = jest.fn() render(<input type="checkbox" ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is a known limitation. The
event.targetis a browser event property. However, many Semantic UI components, such as a Dropdown, Checkbox, and Radio do not work directly with native browser form controls such asinputandselect. They are built using stylized markup and custom internal state.Because of this, there are no native browser events available for certain callbacks. This is why all change events in Semantic-UI-React provide the event first (when available) but also a second argument which contains the data you need. You should never have to access the native browser event for most tasks.
You can see examples of how to retrieve values from the second argument in the docs. Such as the Radio Group example.
Going to close this issue as this is not a bug. Feel free to open another issue if you feel any components are missing helpful data in the second argument and we can add more callback data.
@twang2218 - to add to Levi’s response, in the
<Checkbox>,<Radio>and<Dropdown>components the second argument toonChangewill be an object withname,value, andchecked(if relevant). We’re also considering just passing the whole props object back in addition to those keys (see https://github.com/Semantic-Org/Semantic-UI-React/issues/623).@levithomason - for consistency, what do you think about returning
{ name, value }as the second argument to theonChangehandler? Right now inside aFormcomponent in my app I have:which isn’t ideal. I guess this would actually be addressed if we decided to pass all props back to handlers.