1

I have a div in my application with an id of financial[terms], but jquery does not seem to be able to target this.

Is this a semantically invalid id?

JonoB
  • 5,801
  • 17
  • 55
  • 77

7 Answers7

2

According to w3.org:

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 (".").

So, no, your id is not semantically valid.

Moishe Lettvin
  • 8,462
  • 1
  • 26
  • 40
  • This is interesting. I'm not sure how colons or periods in the id would be interpreted by JQuery, since they're part of JQuery selector syntax. – arviman Nov 01 '11 at 22:50
2

The HTML 4.01 spec states that ID 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 (.).

To precisely answer your question, your id is invalid in HTML 4.01

According to this article, in HTML 5 however, your id looks like it could be valid.

EDIT:

It should be noted that jQuery itself may still have some trouble with certain aspects of a valid HTML 4.01 id, such as periods and colons.

Sparky
  • 98,165
  • 25
  • 199
  • 285
0

Is this a semantically invalid id?

I would say yes, use something more like financial-terms.

Sam
  • 15,336
  • 25
  • 85
  • 148
0

While it's invalid in IDs (but fine in name attributes), you can probably get away with it if you want. You need to escape special characters, as the square brackets have their own meaning in jQuery selectors.

Community
  • 1
  • 1
El Yobo
  • 14,823
  • 5
  • 60
  • 78
0

This is most definitely invalid. An id can start with a letter, and be followed by letters (A-Za-z), digits (0-9), hyphens ("-"), and underscores ("_").

arviman
  • 5,087
  • 41
  • 48
  • ... and colons and periods... you must be getting your information from the w3schools. See [http://www.w3.org/TR/html401/types.html#type-name](http://www.w3.org/TR/html401/types.html#type-name) – Sparky Nov 01 '11 at 23:01
  • I know colons and periods are valid characters in an ID but I'm not sure how they can be used in this context, since they're part of JQuery selector syntax. – arviman Nov 02 '11 at 00:14
  • The answer to this question, _"Is this a semantically invalid id?"_, is straightforward. – Sparky Nov 02 '11 at 00:25
  • I agree. I was just commenting on it's practicality. – arviman Nov 02 '11 at 03:56
0

Id naming rules from W3.org:

  • First character: [A-Za-z]
  • Rest of the characters: [A-Za-z],[0-9],"-","_",":" or "."

Link

Lycha
  • 9,937
  • 3
  • 38
  • 43
  • Another perfect example where W3Schools falls short by providing incomplete information. [See the W3 spec](http://www.w3.org/TR/html401/types.html#type-name). And then read [this](http://w3fools.com). – Sparky Nov 01 '11 at 22:57
  • 2
    @Sparky672 Thank's, I didn't know that before. IMHO they do have the clearest way to describe HTML/CSS specs, too bad the information itself seems to be lacking. My eyes have been opened now! – Lycha Nov 01 '11 at 23:04
  • It's just a pity the real W3 site isn't as easy to use as the W3schools site. But yes, though these days W3schools is much better than it used to be I still avoid it so that I don't have to try to remember which parts are correct and which are not. – nnnnnn Nov 01 '11 at 23:21
  • valid or not, i wouldn't want to name my element as `financial:first` for instance. Especially if i'm working with jquery :p – arviman Nov 02 '11 at 00:16
0

If you can't change the ID, you can still use document.getElementById inside jQuery or (worse performance) use $("[id='financial[terms]'").

Sparky
  • 98,165
  • 25
  • 199
  • 285
AutoSponge
  • 1,444
  • 10
  • 7
  • jQuery aside, it's not valid HTML anyway unless he's using HTML 5. – Sparky Nov 02 '11 at 01:44
  • TBH, validity doesn't matter. Sure it's great to be valid, but if it works in every browser (and it does) there's no reason to do a major refactor if it's like that all over his app. – AutoSponge Nov 02 '11 at 12:40
  • Validity compliance always matters... it's a major factor in keeping your site from breaking when newer browsers are released. Since comments are not for discussion, this is where I'm leaving it. – Sparky Nov 02 '11 at 14:26