WebKit Bug! Default value of unchecked checkbox element is an empty string!
If you drop a checkbox on a page, without setting the value attribute, for example:
<input type="checkbox" />
Browsers should default the value to the string “on”. To check this, add a “onclick” handler to display the value of the checkbox:
<input type="checkbox" onclick="alert(this.value);" />
Now, test this in the latest versions of Internet Explorer, Firefox, Opera, Chrome and Safari, you will notice that for the first three browsers, the value displayed for both checked and unchecked state is “on” but for Chrome (2) and Safari (4), the value displayed for the unchecked state is an empty string!
Surely this is a bug, because if you did specify a value, for example:
<input type="checkbox" value="foo" onclick="alert(this.value);" />
You will see “foo” displayed in the alert box for both checked and unchecked state on all browsers!
According to the spec a checkbox input isn’t valid without a value (http://www.w3.org/TR/html401/interact/forms.html#h-17.4) – though it wouldn’t fail “validation” because you can’t create a constraint like that within a DTD.
I think webkit is more “right” in this case, as the behaviour seems correct from a client-scripting perspective (how annoying would it be if you couldn’t retrieve the value if it wasn’t checked?) and the older engines better from a CGI/legacy perspective. Does the behaviour change for different doctypes/rendering modes?
Hope you are well my friend, it’s a shame you weren’t at Lomax’s wedding!
Owain Cleaver
June 17, 2009 at 10:51 pm