React, how to make a checked checkbox editable

I had a checkbox in a React component:
<input name="enable" type="checkbox" />
and I wanted it to be checked by default, yet the user could change its value.

Using
<input name="enable" type="checkbox" …


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

I had a checkbox in a React component:

<input name="enable" type="checkbox" />

and I wanted it to be checked by default, yet the user could change its value.

Using

<input name="enable" type="checkbox" checked="checked" />

didn’t work. The checkbox state could not be changed.

The solution was to use the defaultChecked attribute:

<input name="enable" type="checkbox" defaultChecked={true} />

If the checkbox needs to be checked depending if the value was checked in a variable (for example in an editing form when you are getting the actual value from the database) you can use

<input name="enable" type="checkbox" defaultChecked={existing_enable_value} />


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-06-16T05:00:00+00:00) React, how to make a checked checkbox editable. Retrieved from https://www.scien.cx/2021/06/16/react-how-to-make-a-checked-checkbox-editable/

MLA
" » React, how to make a checked checkbox editable." flaviocopes.com | Sciencx - Wednesday June 16, 2021, https://www.scien.cx/2021/06/16/react-how-to-make-a-checked-checkbox-editable/
HARVARD
flaviocopes.com | Sciencx Wednesday June 16, 2021 » React, how to make a checked checkbox editable., viewed ,<https://www.scien.cx/2021/06/16/react-how-to-make-a-checked-checkbox-editable/>
VANCOUVER
flaviocopes.com | Sciencx - » React, how to make a checked checkbox editable. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/16/react-how-to-make-a-checked-checkbox-editable/
CHICAGO
" » React, how to make a checked checkbox editable." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/06/16/react-how-to-make-a-checked-checkbox-editable/
IEEE
" » React, how to make a checked checkbox editable." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/06/16/react-how-to-make-a-checked-checkbox-editable/. [Accessed: ]
rf:citation
» React, how to make a checked checkbox editable | flaviocopes.com | Sciencx | https://www.scien.cx/2021/06/16/react-how-to-make-a-checked-checkbox-editable/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.