Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

input checkbox true or checked or yes [duplicate]

I've seen the three implementations of pre-selecting a checkbox. I started off using checked="checked" because I thought it was the "proper" way (never did like the "checked"="yes" however). I am thinking of changing to checked="true" as it seems more readable and is easier to code the JavaScript. Note that this same question applies to other attributes such as "disabled"="disabled" versus "disabled"="true". As long as I am consistent, is using "true" the preferred approach? Thank you

<input type="checkbox" checked="checked" value="123" name="howdy" /> <input type="checkbox" checked="true"    value="123" name="howdy" /> <input type="checkbox" checked="yes"     value="123" name="howdy" /> 
like image 915
user1032531 Avatar asked May 31 '12 11:05

user1032531


People also ask

How check if checkbox is true?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.

Does checkbox return true or false?

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.

Do checkbox inputs only post data if they're checked?

Yes, standard behaviour is the value is only sent if the checkbox is checked. This typically means you need to have a way of remembering what checkboxes you are expecting on the server side since not all the data comes back from the form. The default value is always "on", this should be consistent across browsers.

What is the input type for checkboxes?

Definition and Usage 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

Only checked and checked="checked" are valid. Your other options depend on error recovery in browsers.

checked="yes" and checked="true" are particularly bad as they imply that checked="no" and checked="false" will set the default state to be unchecked … which they will not.

like image 69
Quentin Avatar answered Sep 21 '22 18:09

Quentin