4

Basically a simple question I'm not really sure where to search after;

Is it safe to write checked as an attribute instead of checked="checked" without becoming incompatible with certain browsers? This also includes selected, disabled, readonly and so on.

It just feels like mindless repetition to type in the same thing twice because of habit, so if anyone knows if this can be skipped without any consequences or not, please share your knowledge.

Example:

Can I always use <input type="checkbox" checked /> instead of <input type="checkbox" checked="checked" />?

This answer is answered to a certain extent in another question. But apart from XHTML, I don't see any answers regarding browser compatibility and the like.

8
  • 1
    To be clear, are you referring to HTML attributes as opposed to CSS selectors? Your title uses CSS attribute selector syntax which is not the same thing as HTML attributes, but your tags say [html]. Commented Mar 27, 2014 at 10:06
  • @Nicolò Monili: That question is about DOM scripting, not HTML markup. Not really a dupe. Commented Mar 27, 2014 at 10:08
  • Good answers, however they address what you should type. Not, what I can spot, what you have to type to be functional in most browsers. Commented Mar 27, 2014 at 10:10
  • @BoltClock I added an example to clarify. Just used it's css reference for easier title. Commented Mar 27, 2014 at 10:12
  • 1
    @Robin Castlin: The only difference is HTML vs XHTML syntax. You either use checked and close it with >, or checked="checked" and close it with />. There is literally no other difference. Browsers handle both of them the same way. Anybody telling you to use XHTML markup simply because "it's just good practice" without explaining this difference probably doesn't know what they're talking about. Commented Mar 28, 2014 at 3:42

1 Answer 1

1

When you use the following, you won't be able to get the value of checked as there is no value.

<input type="checkbox" checked /> 

When you use the following, you can get the checked value of the checkbox. This might be helpful in fetching the checkbox checked value using javascript or jQuery.

<input type="checkbox" checked="checked" /> 
Sign up to request clarification or add additional context in comments.

5 Comments

I just tested this in Firefox and Chrome, and both returned the same answer: jsfiddle.net/3HJE4 Are you using another browser?
Yes that would give the same output.
The checked attribute does not have a value. Maybe that's why you can't get one.
you cant get one, because there is no value attribute in input
I minus the answer there because chercked attribute vs value attribute. Value of the input is determinated by the value attribute which represent the value of the input, while the state of the input is represented by the checked attribute.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.