Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the syntax for boolean attributes, e.g. a checked checkbox, in HTML?

Sounds like a bit of a silly question, but I am wondering what is the best way of stating that a checkbox is checked/unchecked in HTML.

I have seen many different examples:

<input type="checkbox" checked="checked" /> <input type="checkbox"  />  <input type="checkbox" checked="yes" /> <input type="checkbox" checked="no" />  <input type="checkbox" checked="true" /> <input type="checkbox" checked="false" /> 

Which browsers work with which ones of these, and most importantly, does jQuery figure out which box is checked in all 3?

Edit: The W3C spec seems to imply that just the checked attr being there is correct. Does that mean that checked="false" and checked="no" will still check the box though?

like image 307
Fiona - myaccessible.website Avatar asked May 20 '10 14:05

Fiona - myaccessible.website


People also ask

What is the syntax for checkbox in HTML?

The <input type="checkbox"> defines a checkbox. The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices. Tip: Always add the <label> tag for best accessibility practices!

What is boolean attribute in HTML?

A Boolean attribute is an attribute that can only be true or false. How does a Boolean attribute work? According to the HTML specification: The presence of a boolean attribute on an element represents the “true” value, and the absence of the attribute represents the “false” value.

How do you put a boolean in HTML?

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.


1 Answers

In HTML:

<input type="checkbox" checked> <input type="checkbox" checked="checked"> 

For XHTML you have to use attribute/value matching pairs:

<input type="checkbox" checked="checked" /> 
like image 65
Andy E Avatar answered Sep 20 '22 20:09

Andy E