views:

305

answers:

5

<div id="example-value"> or <div id="example_value">?

This site and Twitter use the first style. Facebook and Vimeo - the second.

Which one do you use and why?

+3  A: 

I believe this is entirely up to the programmer. You could use camelCase too if you wanted (but I think that would look awkward.)

I personally prefer the dash, because it is quicker to type on my keyboard. So I would say that you should go with what you are most comfortable with, since both your examples are widely used.

adamse
this question is similar and verifies this answer http://stackoverflow.com/questions/70579/what-is-a-valid-value-for-id-attributes-in-html
Matt Smith
+1  A: 

Either example is perfectly valid, you can even throw into the mix ":" or "." as separators according to the w3c spec. I personally use "_" if it is a two word name just because of its similarity to space.

Myles
Yes, you can use colons and periods for Ids, but that's a good way to get the person writing the CSS file to hate you.
Dave Markle
A HTML identifier `ZZ:ZZ` would have to be escaped as `ZZ\00003AZZ` (CSS2 and above).
McDowell
I didn't say it was good practice, I said it was valid.
Myles
Sounds like a fun way to make jQuery explode
Mike Robinson
A: 

I would suggest underscore mainly for the reason of a javascript side-effect I'm encountering.

If you were to type the code below into your location bar, you would get an error: 'example-value' is undefined. If the div were named with underscores, it would work.

javascript:alert(example-value.currentStyle.hasLayout);
jgreep
That should be `document.getElementById("example-value")`, which will work fine.
Chuck
A: 

I use the first one (one-two) because its more readable. For images though I prefer the underscore (btn_more.png). Camel Case (oneTwo) is another option.

Nimbuz
+2  A: 

It really comes down to preference, but what will sway you in a particular direction might be the editor you code with. For instance, the auto-complete feature of TextMate stops at a hyphen, but sees words separated by an underscore as a single word. So class names and ids with the_post work better than the-post when using its auto-complete feature (Esc).

Doug Neiner