7

In HTML, only text controls like text-box, text-area, password-box have read-only property. Why didn't they support check-box/radio-button?

I know I can achieve this feature using script by putting onclick="return false;". (I don't want any workaround, I want to know only the reason for not providing native support to achieve this)

But my question is there any specific reason for not supporting read-only in check-box/ radio button even-though it accepts input from user.

EDIT 1: I found this but it doesn't answer my question - Why there is no native way to restrict the user to change the state without disabling it?

Community
  • 1
  • 1
kriznaraj
  • 484
  • 2
  • 11
  • 2
    How about using the attribute `disabled` ? – Rob Sedgwick May 20 '14 at 06:04
  • Its working readonly option for check box and radio button also – Ramki May 20 '14 at 06:07
  • This sounds like a question better suited to programmers. It's about speculating on the decisions made by language designers a decade+ ago, not a practical problem that can be solved. – Quentin May 20 '14 at 06:07
  • @RossBille — No, it isn't a duplicate of that. – Quentin May 20 '14 at 06:10
  • 1
    @Rob, If i put disabled on postback it will not get submitted – kriznaraj May 20 '14 at 06:10
  • 2
    My question is why didn't give this feature just to prevent changing the state? – kriznaraj May 20 '14 at 06:12
  • sorry, I am not creator of the `markup` :( – shyammakwana.me May 20 '14 at 06:14
  • @Quentin It looks like a dupe to me. – Chris Baker May 20 '14 at 06:31
  • @krishna, yeah, but `readonly` on a checkbox doesn't make sense either ( what exactly are you going to copy? ). Use a hidden field, or un disable on submit. – Rob Sedgwick May 20 '14 at 06:39
  • @krishna, but I can see where you are coming from – Rob Sedgwick May 20 '14 at 06:51
  • 1
    @Chris — The question is asking why there isn't a working native attribute, not what the alternatives are. – Quentin May 20 '14 at 08:13
  • @Quentin The highest voted answer there quotes the same source as the highest voted answer here, which explains pretty clearly *why* -- you can't change the value of a checkbox, so `readonly` is illogical. They are always read only. – Chris Baker May 20 '14 at 14:26
  • @Chris, How it'd be illogical chris! then wat w3c offers me to restrict user not to change the state? what is the native way to restrict user not to change the state? – kriznaraj May 21 '14 at 10:08
  • 1
    The `disabled` property prevents interaction, which includes changing the state. The `readonly` property prevents changing the value, but **allows** interaction - eg, changing the state. Checking a checkbox does not change the *value* of the checkbox, it changes the state. The value is whatever the `value` property is set as, and there is never a circumstance in which the user can edit the value of a checkbox, unlike a textarea for instance. Thus, it is not logical to implement `readonly` on an element that is inherently read-only by design. – Chris Baker May 21 '14 at 14:00
  • @Chris, Ur explanation is pretty clear. Thank u very much. Then If i dont want to disable(because i need the value to be submited on postback) there is no native way to prevent interaction right? Pls corrct me if i'm wrong. – kriznaraj May 21 '14 at 14:18
  • 1
    That's correct. You can either use the `onclick` workaround, or `onsubmit` your form can remove the `disabled` attribute on all the elements really quick. Something to consider: why are you putting a value that you need in a checkbox that the user cannot interact with? Just use a hidden form element. Better still, put the value in session, or the database so the user can't mess with it at all. I can't imagine your use case, it sounds like you might have an [XY problem](http://meta.stackexchange.com/a/66378/163135) -- question your approach. – Chris Baker May 21 '14 at 15:10
  • Can anyone tell, why is this Duplicate not removed still? I edited the Question? Is it still really seems to be duplicate? I really didn't get the answer for my question what i meant? – kriznaraj May 23 '14 at 09:44
  • @Quentin - Suggest me something, I want the duplicate to be removed. – kriznaraj May 23 '14 at 09:45
  • @krishna — Read the answers you've got. Jukka's seems to answer it. Accept it. Stop worrying about the duplicateness. – Quentin May 23 '14 at 09:49
  • @Quentin - No, I'm really asking different question Quentin. Am not asking abt why read-only is not supported here rather, why didn't they consider giving native way for restricting the state change? I agree Jukka's answer is satisfying. I'm still expecting better one. – kriznaraj May 23 '14 at 09:59
  • @krishna — He's got a link to a discussion about it by the spec developers. You're not likely to get a better answer than that without some kind of telepathy + time machine so you can read the minds of everyone involved in the development of browsers and HTML. – Quentin May 23 '14 at 10:00
  • @Quentin - So, Is this site not the right place for asking such question or what? – kriznaraj May 23 '14 at 10:09
  • Technically, this question is not a duplicate of `Can HTML checkboxes be set to readonly?`, because it asks why, not how, which goes to the reasoning for the functionality, and not to how to work around its limitations. – Patanjali Feb 04 '20 at 03:16

4 Answers4

2

It's important to understand that READONLY merely prevents the user from changing the value of the field, not from interacting with the field. For many types of fields, READONLY is irrelevent because you don't normally change the value. In checkboxes, for example, you can check them on or off (thus setting the CHECKED state) but you don't change the value of the field. DISABLED, however, actually prevents you from using the field.

Notice in these examples that you can set the checkboxes even though they are "read only":

SOURCE

EXAMPLE from faqs.org

shyammakwana.me
  • 5,562
  • 2
  • 29
  • 50
2

HTML 4.01 allows readonly for input in general without restrictions by type. However, it seems that it was generally not implemented in browsers, possibly because the implementors did not see much need for it. In HTML5 CR, readonly is explicitly disallowed for input type=checkbox, and HTML5 CR (or some other “HTML5 spec” such as HTML 5.1 Nightly or WHATWG Living HTML) is generally what implementors use as the guideline these days.

There have been proposals to allow it in HTML5, e.g. a discussion in May 2011 at the whatwg list, as well as browser bug reports (rejected on the basis of what HTML5 says), but apparently without sufficiently good use cases. Although real use cases exist, they are probably too marginal, and reasonable alternatives can be presented, such as the use of a checkbox image (checked or unchecked as desired) with suitable associated text and, if the pseudo-checkbox is set to checked, a hidden field carrying the desired information.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
1

Use jquery to make it disable(readonly) or enable. Only text fields are possible to make them readable at the time of rendering.

<input type="checkbox" id="chk1"/>

then by using Jquery

$(document).ready(function(){
$("#chk1").prop('disabled',true);
});
  • The question is asking **why** the feature isn't native, it isn't looking for workarounds (it even mentions a work around in the question itself!). – Quentin May 20 '14 at 06:08
  • You don't have to go through this... would work.. BTW you are missing # in selector – Runcorn May 20 '14 at 06:09
  • @azhar, Even i achecived this feature using some script. but i just want to kno why didnt this feature be implemented and why didn't they design it like just to prevent user to change the state? – kriznaraj May 20 '14 at 06:42
  • @krishna actually textfields are readonly property because we restrict our user to enter any text by making it readonly in case of checkboxes and radiobuttons no text is added by keyboard or any source. If you think about read/write these properties are associated with any type of file and file contain some text(in the form of charcters) while textbox, textarea's, also contains text(in the form of characters) that's why they have readonly property. – azhar_SE_nextbridge May 20 '14 at 06:50
0

First, you shouldn't intersect the onclick event of a checkbox for the mere purpose of checking whether it is checked or not. You should use the onchange event instead. Why? Because the onclick event triggers before the onchange event, so if you intersect the onclick event you will not be able to get the actual state of the checkbox unless you do some ugly javascript acrobatics with setTimeout

If you want to make the checkbox "read-only" you have to use the disabled attribute. Like below...

<input type="checkbox" disabled/>

To answer your question, I think it's better explained by the HTML Specs in W3.Org

The difference between disabled and readonly is that read-only controls are still focusable, so the user can still select the text and interact with it, whereas disabled controls are entirely non-interactive. (For this reason, only text controls can be made read-only: it wouldn't make sense for checkboxes or buttons, for instances.)

Leo
  • 14,625
  • 2
  • 37
  • 55
  • I know the difference what u mentioned, but why didn't they design it like just to prevent user to change the state? – kriznaraj May 20 '14 at 06:34
  • I'm not an advocate of the WWW3 Standards and I didn't even participate in the design of these standards so I can't tell you why or how they arrived to this conclusion/outcome while brainstorming the specs. The explanation seems clear to me...DISABLED = NO INTERACTION...while READ-ONLY = INTERACT BUT DON't CHANGE. If you really understand the difference you wouldn't be asking yourself why – Leo May 20 '14 at 06:45