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.
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.
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.
You can check whether a checkbox is checked or not using checkbox_elemnt. checked attribute.
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"
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With