views:

249

answers:

8

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?

+2  A: 

HTML:

Any of them should be the right one as they say on W3C page. checked attribute just has to be set.

As I wrote in Coronatus' comment:

In fact, it doesn't matter if it's checked, checked="whatever" or checked="checked". It's checking boolean value, so it's either true (set) or false (not set).

XHTML:

You have to specify it, so checked="checked" is the only valid one. Most of the browsers will probably parse HTML values correct, but you'll have error on your page nonetheless.

Ondrej Slinták
+1  A: 

According to W3C recommendations your first suggestion is correct.

hudolejev
A: 

The presence of the "checked" property specifies the status. The value is irrelevant/not needed.

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

is my suggestion

kazanaki
The value does matter - it can only be "checked" (Browsers just error correct for rubbish. Don't depend on error recovery, write error free code instead). If you are writing HTML (as opposed to XHTML) you may (and should) omit the name (and the equals and the quotes) leaving just the value. (Boolean attributes are weird.)
David Dorward
As you saw from the code on the post I am not saying that the value should not be present. I am just saying that you could write checked="hello" or checked="maybe" and still get the desired result.
kazanaki
But yes you are right that we should target XHTML and not just HTML.
kazanaki
+9  A: 

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" />
Andy E
+4  A: 

According to the HTML spec, the correct one is:

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

I use this and jQuery works perfectly with them.

Coronatus
In fact, it doesn't matter if it's checked, checked="whatever" or checked="checked". It's checking boolean value, so it's either true (set) or false (not set).
Ondrej Slinták
+1  A: 

A quick test reveals that jQuery equates any value set to being checked.

alert( $('input:checked').length );
// Returns 5

Even an empty string and the negative values.

patrick dw
A: 

I would be careful using it on chrome, firefox and opera. Chorme seems to have a problem with checkboxes showing up, Firefox is case sensitive with styles and tags, and opera seems to run fine but is slow at updating. Might just be the versions of the browsers being used.

Other than that I believe that it should run <input type="checkbox" checked /> just fine in all of them. The only difference really is how the individual browsers appear to show the content on the page.

Justin Gregoire
+1  A: 

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?

Exactly. That's why it's a bad idea to use checked="true" and checked="yes", they imply that checked="false" and checked="no" will not check the box.

Ms2ger