1

My input checkbox always returns true, when i mark it passsa true correctly, but when i unmark tb i get true. I would like the checkbox to return true when checked and false when unchecked, this way I would apply my logic in the handlesubmit function.


    handleChange = e => {
        const { name, value } = e.target;
        console.log(name, value);
        switch (name) {

          case 'period': this.json.schedule = 'period'; break;

    }


 <input
       type="checkbox"
       name="period"
       defaultValue
       onChange={this.handleChange}
 />

4
  • I added the function on change Commented Apr 16, 2019 at 20:00
  • Your onChange callback function isnt returning anything, where are you checking for true and false? Commented Apr 16, 2019 at 20:05
  • I think I understood the operation of the wrong component, believe that the simple fact of unmarking would return false, instead of true as I'm always receiving. Commented Apr 16, 2019 at 20:07
  • add checked to const { name, value } = e.target Commented Apr 17, 2019 at 17:23

4 Answers 4

10

Checkout the value of e.target.checked.

In your case, change this line: const { name, value } = e.target to include checked.

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      checked: true
    }
  }
  
  handleChange = (e) => {
    const { checked } = e.target
    this.setState({
      checked: checked
    })
  }
  
  render() {
    return (
      <div>
        <input type="checkbox"
               onChange={e => this.handleChange(e)}
               defaultChecked={this.state.checked}/>
        {this.state.checked.toString()}
      </div>
    )
  }
}

ReactDOM.render((<App/>), document.getElementById('testing'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="testing"></div>

Sign up to request clarification or add additional context in comments.

Comments

2

You can use useState.

import React, { useState } from 'react';

const App = () => {
    const [Check, setCheck] = useState(false);
    return (
        <div>
            <h1>{Check ? 'Checked' : 'Not checked'}</h1>
            <input type="checkbox" onChange={(e) => setCheck(e.target.checked) }/>
        </div>
    );
};

export default App;

Comments

0

You first need to define what check is considered? If it is check it is true and when it is not checked it is false. Here is some code to get you started.

{   this.state.data.map(function(item, index) { return (
    <input type="checkbox" checked={item.value} onChange={this.handleChange.bind(this, index)}/>
  );
      }.bind(this))
}

1 Comment

I added the function on change
0

You aren't checking wether the box is checked or not, try:

handleChange = e => {
    if (document.getElementByClassName("period").checked) {
        // box is checked
    } else {
        // box is unchecked
    }
}

4 Comments

I added the function on change
Thanks for that, I made an edit as well. I would keep things simple, readability is always important.
Where would I add this instruction?
you can use e.target.checked instead

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.