'event.target.value' are 'undefined' of 'onChange(event)' for 'Checkbox', 'Radio', and 'Dropdown'

See original GitHub issue

Details

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:closed
  • Created 7 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

17reactions
levithomasoncommented, Oct 6, 2016

This is a known limitation. The event.target is 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 as input and select. 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.

5reactions
jeffcarbscommented, Oct 6, 2016

@twang2218 - to add to Levi’s response, in the <Checkbox>, <Radio> and <Dropdown> components the second argument to onChange will be an object with name, value, and checked (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 the onChange handler? Right now inside a Form component in my app I have:

handleDropdownChange (e, result) {
  const { name, value } = result
  // ...
}

handleInputChange (e) {
  const { name, value } = e.target
  // ...
}

which isn’t ideal. I guess this would actually be addressed if we decided to pass all props back to handlers.

Read more comments on GitHub >

github_iconTop 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 >

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