HTML
Web Development
Programming
Checkboxes
Readonly

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:

html
    <input type="checkbox" id="myCheckbox" checked onclick="return false;">

Or flip the state back whenever a change is attempted:

html
    <input type="checkbox" id="myCheckbox" checked onclick="this.checked=!this.checked;">

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:

html
    <input type="checkbox" id="myCheckbox" disabled checked>
    <input type="hidden" name="myCheckbox" value="1">

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:

css
1    input[type="checkbox"][readonly] {
2      pointer-events: none;
3      opacity: 0.6;
4    }

Comparison of checkbox states

AttributeAllows ChangeIncluded in SubmissionUse Case
NoneYesYesStandard checkbox input
readonlyYesYesNot applicable to checkboxes
disabledNoNoWhen input should not be sent to the server
JavaScriptNoYesTo simulate readonly while sending data
Hidden FieldNoYesTo send data, simulating disabled appearance

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Data 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.

Practice algorithms