0

Please see the following code at first. I want the particular checkbox (with id cfc1) to not be shown ticked whenever clicking on it. I have used the onCheck function there. I'm not finding any way to the particular element be shown unchecked whenever clicked on it. I'm trying using the code -

if(e.currentTarget.id === 'cfc1') {
    document.getElementById(e.currentTarget.id).checked = false; 
}

but it's not working.

How to do it using the onCheck function?

import React, {Component} from 'react';
import Checkbox from 'material-ui/Checkbox';

class Checkboxes extends Component {
  constructor() {
     super();
     this.state = {
        checkids:['cfc0', 'cfc1', 'cfc2', 'cfc3']
     };
     this.handleCheck = this.handleCheck.bind(this);
     this.getElements = this.getElements.bind(this);
  }
  getElements() {
     let arr = [];
     this.state.checkids.forEach(function(element) {
        arr.push(
          <Checkbox
             id={element}
             onCheck={this.handleCheck}
          />
        );
     }, this)
     return arr;
  }
  handleCheck(e) {
     console.log(e.currentTarget.id);
     console.log(e.currentTarget.checked);
  }
  render() {
     return(
       <div>
          {this.getElements()}
       </div>
     );
  }
}
export default Checkboxes

2 Answers 2

1

To get it working for your use case, try having a state variable that maintains the checked information of each element id.

And in the onCheck handler toggle the checkbox for all the boxes except the one desired by setting the state.

WORKING DEMO

class App extends Component {
  constructor() {
    super();
     this.state = {
        checkids:{
          'cfc0': false,
          'cfc1': false, 
          'cfc2': false,
          'cfc3': false
        }
     };
  }

  handleCheck(e) {
     const checkids = {...this.state.checkids};
     checkids[e.target.id] = e.target.id === "cfc1" ? false : !checkids[e.target.id];

     this.setState({checkids});
  }

  getElements() {
     let arr = [];
     Object.keys(this.state.checkids).forEach(function(element, index) {
        arr.push(
          <Checkbox
             key={index}
             id={element}
             checked={this.state.checkids[element]}
             onCheck={this.handleCheck.bind(this)}
          />
        );
     }, this)
     return arr;
  }

  render() {
    return (
      <MuiThemeProvider>
        <div>
          {this.getElements()}
       </div>
      </MuiThemeProvider>
    );
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

sorry, I have edited my question a little. how to do it using the onCheck function ?
I'm trying using this code inside the handleCheck function. but it's not working. - if(e.currentTarget.id === 'cfc1') { document.getElementById(e.currentTarget.id).checked = false; }
That won't work, because the first time it executes it checks it. I will add an update soon but what is your use case that is requiring you to only do it in the onCheck handler?
yeah. Thanks for your concern friend. That's why just now I have edited the question again. I'm actually having this kind of issue.
Updated the answer, please have a look.
|
0

You can use this.checked = false via javascript

document.getElementById('cfc1').onclick = function() {
  this.checked = false;
}
<label><input type="checkbox" id="cfc1">Click</label>

2 Comments

Can you do this thing by using the onCheck function event handlers?
I'm trying using this code inside the handleCheck function. but it's not working. - if(e.currentTarget.id === 'cfc1') { document.getElementById(e.currentTarget.id).checked = false; }

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.