Question

So, I've been fixing up my website. My website of course generates HTML from a "view".

Right now, a portion of my view looks like this:

<input type="checkbox" name="Publish" checked="{=Entry.Publish ? "yes" : "no" =}" value="true" />

This is the easiest way to go about this. However, when it generates checked="no", the checkbox will still be checked by default whenever I load the page. Do I really have to exclude the checked attribute all together for it to not be checked?

Also, I'm using HTML5 as my doctype.

Was it helpful?

Solution

Short version: yes, it needs to be excluded.

The value of the attribute is irrelevant, as long as it is present, the box will be checked.

<input type="checkbox" name="Publish" value="true" checked />

This is valid in HTML5.

In XHTML, the attribute needed a value and the convention was checked="checked" since values like "yes" or "true" implied that the opposites would uncheck the box, which is not true and would confuse beginners. Similar conventions were adopted for readonly="readonly" and disabled="disabled".

OTHER TIPS

Yes. checked is a bool attribute that is "off" when it's absent and "on" when it's present.

W3C reference for boolean attributes and for checked attribute.

from the spec

checked = "checked" or "" (empty string) or empty

Specifies that the element represents a selected control.

You want to include checked if it's to be checked, otherwise omit the attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top