tags:

views:

8764

answers:

16

When creating id attributes for html elements what rules are there for the value?

A: 

Must start with a-z

redsquare
+28  A: 

From the HTML 4 specification:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

A common mistake is to use an ID that starts with a digit.

Peter Hilton
This was useful; thanks!
Pandincus
+1 for linking to the spec.
cletus
A: 

Names can also contain "$" I believe

Slace
+2  A: 

They must also be unique for the page.

Garry Shutler
+3  A: 

Strictly it should match

[A-Za-z][-A-Za-z0-9_:.]+

But jquery seams to have problems with colons so it might be better to avoid that.

Mr Shark
A: 

From the HTML 4 spec...

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

EDIT: d'oh! Beaten to the button, again!

Steve Morgan
+4  A: 

In practice many sites use id attributes starting with numbers, even though this is technically not valid HTML.

The HTML 5 draft specification loosens up the rules for the id and name attributes: they are now just opaque strings which cannot contain spaces.

pdc
+1  A: 

Also, never forget that and ID is unique. Once used, the ID value may not appear again anywhere in the document.

You may have many ID's, but all must have a unique value.

On the other hand, there is the class-element. Just like ID, it can appear many times, but the value may be used over and over again.

Vordreller
+6  A: 

You can technically use colons and periods in id/name attributes, but I would strongly suggest avoiding both.

In CSS (and several JavaScript libraries like jQuery), both the period and the colon have special meaning and you will run into problems if you're not careful. Periods are class selectors and colons are pseudo-selectors (eg., ":hover" for an element when the mouse is over it).

If you give an element the id "my.cool:thing", your CSS selector will look like this:

#my.cool:thing { ... /* some rules */ ... }

Which is really saying, "the element with an id of 'my', a class of 'cool' and the 'thing' pseudo-selector" in CSS-speak.

Stick to A-Z of any case, underscores and minuses (they're not technically hyphens). And as said above, make sure your ids are unique.

That should be your first concern.

Michael Thompson
You can use colons and periods - but you'll need to escape them using double backslashes, eg:$('#my\\.cool\\:thing')or escaping a variable:$('#'+id.replace(/\./,’\\.’).replace(/\:/,’\\:’))http://groups.google.com/group/jquery-en/browse_thread/thread/ba072168939b245a
joeformd
A: 

Older versions of Netscape had problems with "" in names/elements, so I've stuck to A-Z, a-z, 0-9 and "-" in my IDs out of habit. I'd stretch to ""s, but I haven't had any real reason to use them. Shrugs

+1  A: 

Had problems with underscores*, that is. The underscores seem to mark things as italic around here; my bad for not knowing markdown. :)

+44  A: 

As pointed out in other responses, the answer is technically:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

However, as a practical matter, you will be somewhat more limited if you want your documents to work with a variety of browsers, CSS editors, and JavaScript frameworks.

As noted in other responses, jQuery has problems with ids that contain periods and colons.

A more subtle problem is that some browsers have been known to mistakenly treat id attribute values as case-sensitive. That means that if you type id="firstName" in your HTML (lower-case 'f') and .FirstName { color: red } in your CSS (upper-case 'F'), a buggy browsers will not set the element's color to red. Because both definitions use valid characters for the id, you will receive no error from a validation tool.

You can avoid these problems by strictly sticking to a naming convention. For example, if you limit yourself entirely to lower-case characters and always separate words with either hyphens or underscores (but not both, pick one and never use the other), then you have an easy-to-remember pattern. You will never wonder "was it 'firstName' or 'FirstName'?" because you will always know that you should type "first_name".

dgvid
Note that class and id attributes *are* case-sensitive in XHTML, all other attributes are not. Eric Meyer mentioned this in a CSS workshop I attended.
John Topley
Also note that if you try to write a CSS rule to target an element by ID, and the ID beings with a number, it won't work. Bummer!
Zack Mulgrew
As for '.' or ':' in an ID using jQuery, see the jQuery [FAQ](http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_select_an_element_by_an_ID_that_has_characters_used_in_CSS_notation.3F). It contains a small function that does the necessary escaping.
Wolfram
I've also had trouble with using commas in the jQuery selector.
Will Shaver
+3  A: 

jQuery does handle any valid ID name. You just need to escape metacharacters (i.e., dots, semicolons, square brackets...). It's like saying that JavaScript has a problem with quotes only because you can't write

var name = 'O'Hara';

Selectors in jQuery API (see bottom note)

Álvaro G. Vicario
+1 for 'O'Hara' ;)
aSeptik
+1  A: 

It appears that although colons (:) and periods (.) are valid in the HTML spec, they are invalid as id selectors in CSS so probably best avoided if you intend to use them for that purpose.

lstg
A: 

Hyphens, underscores, periods, colons, numbers and letters are all valid for use with CSS and JQuery. The following should work but it must be unique throughout the page and also must start with a letter [A-Za-z].

Working with colons and periods needs a bit more work but you can do it as the following example shows.

<html>
<head>
<title>Cake</title>
<style type="text/css">
    #i\.Really\.Like\.Cake {
        color: green;
    }
    #i\:Really\:Like\:Cake {
        color: blue;
    }
</style>
</head>
<body>
    <div id="i.Really.Like.Cake">Cake</div>
    <div id="testResultPeriod"></div>

    <div id="i:Really:Like:Cake">Cake</div>
    <div id="testResultColon"></div>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript">
        $(function() {
            var testPeriod = $("#i\\.Really\\.Like\\.Cake");
            $("#testResultPeriod").html("found " + testPeriod.length + " result.");

            var testColon = $("#i\\:Really\\:Like\\:Cake");
            $("#testResultColon").html("found " + testColon.length + " result.");
        });
    </script>
</body>
</html>
blacksun1
A: 

I got hit with this one while entering info in a form and it made me curious so I looked it up. Thanks!

registy cleaner