Can an HTML checkbox be readonly?
Codemia Knowledge Hub ·
No. The readonly attribute has no effect on checkboxes, so the user can still toggle a checkbox marked readonly. To get read-only behavior, block the click with JavaScript, or use disabled together with a hidden input so the value is still submitted.
readonly vs disabled on checkboxes
A common need is a checkbox that shows a state the user should not change. On text fields, the readonly attribute prevents users from modifying the entered value, but it does not apply to checkboxes: setting it has no effect, and the checked state can still be changed.
disabled does stop the user from changing the checked state, but disabled checkboxes are not included in the form data sent to the server. If the checkbox carries part of the input you need, disabling it loses that data.
Blocking the click with JavaScript
Capture the click event and return false, which cancels it so the checked state never changes:
Or flip the state back whenever a change is attempted:
Using disabled plus a hidden field
If you go with disabled, the checkbox appears disabled (and so read-only) to the user. Add a hidden field that holds the checkbox's value, so it is still sent as part of the form data:
Styling a checkbox to look read-only
CSS can remove the visual feedback of an interactive checkbox, so it appears read-only without being disabled. This rule targets checkboxes carrying the readonly attribute; pair it with JavaScript to actually prevent changes:
Comparison of checkbox states
| Attribute | Allows Change | Included in Submission | Use Case |
| None | Yes | Yes | Standard checkbox input |
readonly | Yes | Yes | Not applicable to checkboxes |
disabled | No | No | When input should not be sent to the server |
| JavaScript | No | Yes | To simulate readonly while sending data |
| Hidden Field | No | Yes | To send data, simulating disabled appearance |
Related reading
- Can HTML script defer execute before script async?
- Can I create links with 'target=_blank' in Markdown?
- Can I have multiple .finally as well as multiple .catch in Promise.all?
- can I limit consumption of kafka-node consumer?
- Can I load JavaScript code after the rest of page loads?
- Can I set subject/content of email using mailto:?
- Can I set text box to readonly when using Html.TextBoxFor?
- Can I specify RabbitMQ credentials in node.js?
System design interview problems on this topic
- Design Google Doc: solution and walkthrough
- Design Facebook Messenger: solution and walkthrough
- Design Typeahead Suggestion: solution and walkthrough
- Design an Online Presence Indicator Service: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.