How to store the value of a checked checkbox in an array in React.js


Jason 000

I am using react-checkbox-group from " https://www.npmjs.com/package/react-checkbox-group " . I want to add 4 checkboxes on a web application, each checkbox has a different value. Then, I want to store these values ​​in an array. I have the code below for what I want to do, but not sure how to handle the "onChange" event. Now the array 'baseballItems' initially has all the items, I want the unchecked items to disappear, but I'm willing to change it so it's empty and the checked boxes are populating the array.

`this.state = { baseballItems: ['bat','baseball', 'shoes', 'cap']}

...

<CheckboxGroup
checkboxDepth={2}
name="baseballItems"
value={this.state.baseballItems}
onChange={(event) => this.setState({ baseballItems: [event.target.isChecked]})}>
<label><Checkbox value="bat"/> bat</label>
<label><Checkbox value="baseball"/> baseball</label>
<label><Checkbox value="shoes"/> shoes</label>
<label><Checkbox value="cap"/> cap</label>
</CheckboxGroup>`
m51
this.state = { baseballItems: ['bat','baseball', 'shoes', 'cap']}

...

<CheckboxGroup
  checkboxDepth={2}
  name="baseballItems"
  value={this.state.baseballItems}
  onChange={(items) => { this.setState({ baseballItems: items }); }}
>
    <label><Checkbox value="bat"/> bat</label>
    <label><Checkbox value="baseball"/> baseball</label>
    <label><Checkbox value="shoes"/> shoes</label>
    <label><Checkbox value="cap"/> cap</label>
</CheckboxGroup>

There is a similar example in the READMEreact-checkbox-group

Related


How to hide or show div if checkbox is checked in React JS

Kevin Lopez How can I show/hide a div based on checkbox state (checked - unchecked) in React JS, I'm new to React, I know how to do it in jquery, but on React is another way. Thanks in advance. edited To show/hide the div with className="showhidediv" (if the c

Save checked checkbox value as array

Hales Magic I was wondering if anyone could help me. I'm trying to save the value of the checked checkboxes in the wordpress metabox as an array to the database. So far I've been able to save each of them as a separate column, but I just want to compile them i

How to get checkbox value and store in array

Snow In my project I want to insert checkbox values into an array. Here is my code: $('#fpdId').click(function(){ var files = new Array(); //xzyId is table id. $('#xzyId tbody tr input:checkbox').each(function() { if (this.checked) {

How to hide or show div if checkbox is checked in React JS

Kevin Lopez How can I show/hide a div based on checkbox state (checked - unchecked) in React JS, I'm new to React, I know how to do it in jquery, but on React is another way. Thanks in advance. edited To show/hide the div with className="showhidediv" (if this

How to save checkbox checked value in local storage in React?

Vinay I am trying to add checkboxes in a web application. Check boxes are displayed on web pages. I want to save the checkbox checked value in local storage, so I can keep the checkbox checked on page refresh. How can I do this? { Object.keys(catagory_produc

Store only .checked checkbox value in state?

Dean Arvin I have several checkboxes, onClick, that are pushed to the state via .concat(). class App extends React.Component { constructor() { super() this.state = { selectedChoices: [] } this.handleClick = this.handleClick.bind(this)

How to store all checked checkbox class names in an array?

cat ya I am trying to store all checked checkbox class names in an array to parse it later using an ajax function. But first make sure all the classes are properly stored in the array, I'll warn them but know, all I see is the first checked checkbox class Sinc

Laravel how to store value based on checked checkbox

Ayama I am trying to store 1 if the checkbox is checked, 0 otherwise. form is <x-checkbox name="visibility" id="visibility">{{ __('Make this visible') }}</x-checkbox> In the controller it only stores the first value of the condition. This always stores 0. $re

How to store if CheckBox is checked?

Daniele If the "don't show again" checkbox is checked, my activity doesn't need to show anymore, but I don't know how to make it work. Here's what I've tried so far: public class PopUpInfoActivity extends Activity { static final String PREFS = "preference_file

How to save checkbox checked value in local storage in React?

Vinay I am trying to add checkboxes in a web application. Check boxes are displayed on web pages. I want to save the checkbox checked value in local storage, so I can keep the checkbox checked on page refresh. How can I do this? { Object.keys(catagory_produc

How to store all checked checkbox class names in an array?

cat ya I am trying to store all checked checkbox class names in an array to parse it later using an ajax function. But first make sure all the classes are properly stored in the array, I'll warn them but know, all I see is the first checked checkbox class Sinc