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.

Community
  • 1
  • 1
Robin Castlin
  • 10,956
  • 1
  • 28
  • 44
  • 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]. – BoltClock Mar 27 '14 at 10:06
  • @Nicolò Monili: That question is about DOM scripting, not HTML markup. Not really a dupe. – BoltClock Mar 27 '14 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. – Robin Castlin Mar 27 '14 at 10:10
  • @BoltClock I added an example to clarify. Just used it's css reference for easier title. – Robin Castlin Mar 27 '14 at 10:12
  • OK, because the answer differs greatly if you were asking about CSS. – BoltClock Mar 27 '14 at 10:12
  • it is good practice to write in this format. `` – Kheema Pandey Mar 27 '14 at 10:33
  • I've heard it's good practice, but for what reason given? It just feels repetitive and unnecessary. – Robin Castlin Mar 27 '14 at 11:09
  • 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. – BoltClock Mar 28 '14 at 03:42

1 Answers1

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" />
Shanky
  • 331
  • 1
  • 4
  • 17