Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 231
Earlz Avatar asked Nov 26 '12 01:11

Earlz


People also ask

Is checked attribute on checkbox?

The checked attribute is a boolean attribute. When present, it specifies that an <input> element should be pre-selected (checked) when the page loads. The checked attribute can be used with <input type="checkbox"> and <input type="radio"> . The checked attribute can also be set after the page load, with a JavaScript.

What is correct way to insert a checkbox in HTML?

The simplest way to create a checkbox in HTML is by using the input tag. We have set the input type to “checkbox” as you can see in the example code. The name attribute lets us give a name to the checkbox, and with the value attribute, we specify the value it holds.

How do you validate a check box?

You can check whether a checkbox is checked or not using checkbox_elemnt. checked attribute.


2 Answers

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

like image 170
Dennis Avatar answered Oct 02 '22 02:10

Dennis


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.

like image 22
Tieson T. Avatar answered Oct 02 '22 02:10

Tieson T.