32

When I submit a HTML form with a checked checkbox that doesn't have an explicitly defined value, Chrome sends on as a value for that field.

Is that the standard behavior? Or what can I expect from other browsers?

3
  • Yes it is standard! The checked value will be on in every browser Commented Oct 16, 2012 at 9:44
  • @BhuvanRikka: Do you have a reference? Commented Oct 16, 2012 at 12:40
  • 1
    Checked for it but in vain. Couldn't find any. Just saying from self experience Commented Oct 16, 2012 at 12:42

3 Answers 3

29

The HTML 4.01 specification does not specify the value of a checked checkbox. It just refers it saying that it is “on”, but this is just prose and does not say what the default value is. But it also says (under the description of the input element) that the value attribute is required in this case.

So <input type=checkbox name=foo> has undefined behavior as regards to the value used, though in practice browsers use value=on as the default.

Sign up to request clarification or add additional context in comments.

1 Comment

As per HTML 5, if the browser accesses the value via the DOM/IDL (or if the browser creates the DOM and gives access to the value, for example to javascript), the behavior is not undefined, but defined. The value is "on" then. This detail was unspecified in HMTL DOM in the past as the IDL didn't specify this case that detailed. In practice as you write this technically was always the case but remained undefined. stackoverflow.com/a/19094549/367456 - Valid HTML in the past normally required to write that attribute (e.g. HTML 2.0), since HTML 5 this is not necessary any longer.
10

HTML Living Standard reflects this: The value is in mode "default/on", that means it's value is "on" if no value attribute is provided. From 4.10.7.1.16 Checkbox state (type=checkbox) - HTML Living Standard (Sep 2013):

[...]

default/on
On getting, if the element has a value attribute, it must return that attribute's value; otherwise, it must return the string "on". On setting, it must set the element's value attribute to the new value.

This is quite identically also part of another HTML specification, the W3C HTML 5 Aug 2013 Recommendation Specification has this as well:


For reference my earlier comment:

Firefox (Sep 2013), Chrome (Sep 2013), Internet Explorer (6): "on". I suspect this goes back a long way. http://lxr.mozilla.org/classic/source/lib/layout/layform.c#86 - as most browsers need to have some default value for their own code objects I guess this "on" is just common.

Comments

3

Browsers will send the value of a checkbox (in the POST data) only if it is checked. A check to see if a value (any value) for a particular checkbox is present in the POST data is all you need.

i.e.

// no need to check against 'on', 'true', '1' etc..
if(post data contains a value for checkbox1) {
    // checkbox 1 is checked
}
else {
    // not checked
}

2 Comments

mentioned because the actual value is immaterial and shouldn't be checked against ideally.
Even in 2012 Single page applications were a thing, making your comment invalid, we don't send actual form posts this way to the browser we want to get the value of the checkbox sometimes.

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.