1

I'm trying to use the javascript function .checked() in React.js like this :

componentDidMount () {
    document.getElementById("myCheck").checked = true;
}

But I have an error in my console : "la propriété 'checked' n'existe pas sur le type HTMLlement"

How can I use this in React.js ?

Thank you

4
  • In React you have to update the state to re-render the component. Please post more code. Commented Aug 23, 2017 at 9:45
  • 4
    why you want to do it like this? react provides a better way of controlling elements, simply use <input checked={true} onChange={....}/> for controlled component, if you want to use uncontrolled component then use <input defaultChecked={true}/> Commented Aug 23, 2017 at 9:46
  • I would rely on your component state for the checkbox status, instead of modifying any properties of the DOM by hand. Take a look to this question and answer, since it handles a really similar problem. Commented Aug 23, 2017 at 9:50
  • 1
    @MayankShukla Thank you for your answer , it was very simple .. and I didn't think about it ! Thank you Commented Aug 23, 2017 at 9:57

1 Answer 1

1

It worked for me

<div className='container'>
    <input type='checkbox' ref='myAwesomeCheckbox' id={'scales'} />
    <label fhtmlForor="scales">Scales</label>
</div>

componentDidMount = () => {
  this.refs.myAwesomeCheckbox.checked = true;
}
Sign up to request clarification or add additional context in comments.

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.