Search code examples
htmlvalidationcheckbox

Is checked=no valid in HTML checkboxes?


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.


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".