1

I am not able to check/uncheck my checkbox after setting its value.

I've tried using both onClick and onChange but neither work. Not sure what else to try. I also bound it in the constructor.

<section id="publish-section-5">
  <div class="Grid">
    <div class="Col Col--6">
      <div class="grid-block">
        <div class="Form-group publish-insights-input">
          <div class="Form-checkbox is-restricted">                        
              <input
              id="isRestricted"
              name="checkboxDefault"
              type="checkbox"
              checked={this.state.insightsDTO['isRestricted'] == 'Y'}
              onClick={this.handleCheckbox}
            />  
            <label for="restricted">Is Restricted</label>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>        
handleCheckbox(e) {
  this.setState({
    insightsDTO: {
      ...this.state.insightsDTO, 
      [e.target.id]: e.target.checked
    }
  })
}  

I just want to be able to click/unclick it, even after the value gets set.

2
  • Can you share with us what your original state looks like? Commented Sep 9, 2019 at 4:12
  • You're comparing the state to 'Y', but setting it to checked, which will be true or false. Commented Sep 9, 2019 at 4:26

2 Answers 2

1

ANSWER:

import React from 'react';

class Hello extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      insightsDTO: {
        checkboxDefault: true,
      },
    }
  }

  handleCheckbox = (e) => {
    this.setState({
      insightsDTO: {
        ...this.state.insightsDTO,
        [e.target.name]: !this.state.insightsDTO[e.target.name],
      },
    })
  }

  render() {
    return (
      <section id="publish-section-5">
        <div class="Grid">
          <div class="Col Col--6">
            <div class="grid-block">
              <div class="Form-group publish-insights-input">
                <div class="Form-checkbox is-restricted">                        
                    <input
                    id="isRestricted"
                    name="checkboxDefault"
                    type="checkbox"
                    checked={this.state.insightsDTO.checkboxDefault}
                    onChange={this.handleCheckbox}
                  />  
                  <label for="restricted">Is Restricted</label>
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>
    )
  }
};

export default Hello;
Sign up to request clarification or add additional context in comments.

2 Comments

I didn;t check the complete code. I stand corrected.
@Annah you can use either id or name but using name is more conventional.
0

You're setting 'isRestricted' to true or false:

// isRestricted: true
[e.target.id]: e.target.checked

But then testing it against 'Y':

// always false, because the value is never 'Y'
checked={this.state.insightsDTO['isRestricted'] == 'Y'}

Try changing it to:

checked={this.state.insightsDTO['isRestricted']}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.