Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the proper value for a checked attribute of an HTML checkbox?

We all know how to form a checkbox input in HTML:

<input name="checkbox_name" id="checkbox_id" type="checkbox"> 

What I don't know -- what's the technically correct value for a checked checkbox? I've seen these all work:

<input name="checkbox_name" id="checkbox_id" type="checkbox" checked>      <input name="checkbox_name" id="checkbox_id" type="checkbox" checked="on">      <input name="checkbox_name" id="checkbox_id" type="checkbox" checked="yes">      <input name="checkbox_name" id="checkbox_id" type="checkbox" checked="checked">      <input name="checkbox_name" id="checkbox_id" type="checkbox" checked="true">

Is the answer that it doesn't matter? I see no evidence for the answer marked as correct here from the spec itself:

Checkboxes (and radio buttons) are on/off switches that may be toggled by the user. A switch is "on" when the control element's checked attribute is set. When a form is submitted, only "on" checkbox controls can become successful. Several checkboxes in a form may share the same control name. Thus, for example, checkboxes allow users to select several values for the same property. The INPUT element is used to create a checkbox control.

What would a spec writer say is the correct answer? Please provide evidence-based answers.

like image 932
buley Avatar asked Oct 21 '11 15:10

buley


People also ask

What is the value of a checked checkbox in HTML?

The Input Checkbox defaultChecked property in HTML is used to return the default value of checked attribute. It has a boolean value which returns true if the checkbox is checked by default, otherwise returns false.

What is value attribute in checkbox?

The value attribute defines a value which is sent by a POST request (i.e. You have an HTML form submitted to a server). Now the server gets the name (if defined) and the value. <form method="post" action="urlofserver"> <input type="checkbox" name="mycheckbox" value="1">Is it worth?</ input> </form>

Which attribute is used to check the 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 the correct HTML for checkbox?

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!


1 Answers

Strictly speaking, you should put something that makes sense - according to the spec here, the most correct version is:

<input name=name id=id type=checkbox checked=checked> 

For HTML, you can also use the empty attribute syntax, checked="", or even simply checked (for stricter XHTML, this is not supported).

Effectively, however, most browsers will support just about any value between the quotes. All of the following will be checked:

<input name=name id=id type=checkbox checked> <input name=name id=id type=checkbox checked=""> <input name=name id=id type=checkbox checked="yes"> <input name=name id=id type=checkbox checked="blue"> <input name=name id=id type=checkbox checked="false"> 

And only the following will be unchecked:

<input name=name id=id type=checkbox> 

See also this similar question on disabled="disabled".

like image 118
13 revs, 2 users 90% Avatar answered Oct 10 '22 18:10

13 revs, 2 users 90%