0

Given

const  div = document.createElement("div");

div.id = 111;

document.body.appendChild(div);

try {
  console.log(document.querySelector("#111"));
} catch(e) {
  console.error(e); 

  /* 
     Chromium:

     DOMException: Failed to execute 'querySelector' on 'Document':
     '#111' is not a valid selector.

     Firefox
     SyntaxError: '#111' is not a valid selector

  */
}

try {
  console.log(document.querySelectorAll("#111"));
} catch(e) {
  console.error(e); 

  /* 
     Chromium:

     DOMException: Failed to execute 'querySelector' on 'Document':
     '#111' is not a valid selector.

     Firefox
     SyntaxError: '#111' is not a valid selector

  */
}

however, document.getElementById() returns the element

const  div = document.createElement("div");

div.id = 111;

document.body.appendChild(div);

try {
  console.log(document.getElementById("111"));
} catch(e) {
  console.error(e); 
}

References:

6.2 SGML basic types

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

7.5.2 Element identifiers: the id and class attributes

Attribute definitions

id = name [CS] This attribute assigns a name to an element. This name must be unique in a document.

DOM - Living Standard

While this specification defines requirements for class, id, and slot attributes on any element, it makes no claims as to whether using them is conforming or not.

HTML - Living Standard

The id attribute specifies its element's unique identifier (ID).

There are no other restrictions on what form an ID can take; in particular, IDs can consist of just digits, start with a digit, start with an underscore, consist of just punctuation, etc.

Document.querySelector()

Throws a SYNTAX_ERR exception if the specified group of selectors is invalid.

Document.querySelectorAll()

Throws a SYNTAX_ERR exception if the specified group of selectors is invalid.


Is "111" a valid HTML id attribute or are document.querySelector() and document.querySelectorAll() correctly throwing syntax errors?

guest271314
  • 1
  • 15
  • 104
  • 177
  • Possible duplicate of [What are valid values for the id attribute in HTML?](https://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html) – Thilo Aug 05 '17 at 08:27
  • 1
    CSS is more strict than HTML5, see the linked duplicate. – Thilo Aug 05 '17 at 08:28
  • 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 ("."). as you have seen the rules, change your it to start with a alphabet and not with a number and try – Thennarasan Aug 05 '17 at 08:30
  • 1
    @Thilo The present Question is not a duplicate of linked Question. Neither `.querySelector()` nor `.querySelectorAll()` appear within the text of the Question nor at any posted Answer. The linked answers do not address present inquiry. – guest271314 Aug 05 '17 at 08:31
  • 1
    @guest271314: these methods work on CSS selectors. So the rules for that should be covered by the CSS specification (which does not allow identifiers starting with numbers, as per the linked duplicate) – Thilo Aug 05 '17 at 08:33
  • @Thilo That does not address `.querySelector()` or `.querySelectorAll()` portion of Question – guest271314 Aug 05 '17 at 08:34
  • 4
    Isn't it both, and not either or. It (the id value) by the html spec is valid, but by css spec is invalid as [css identifiers cannot start with numbers](https://www.w3.org/TR/CSS21/syndata.html#value-def-identifier) and since that is what querySelector* uses it correctly throws an error – Patrick Evans Aug 05 '17 at 08:35
  • @Thilo CSS does allow identifiers starting with numbers - the numbers must be quoted to be applicable at a `.css` file or within ` – guest271314 Aug 05 '17 at 08:35
  • @PatrickEvans CSS selectors can start with numbers, the numbers need to be quoted at CSS selectors to be applicable – guest271314 Aug 05 '17 at 08:37
  • @guest271314: Try quoting them into your selector (if that is possible somehow) to avoid the syntax error then. – Thilo Aug 05 '17 at 08:37
  • @Thilo At a CSS file or ` – guest271314 Aug 05 '17 at 08:39
  • @PatrickEvans Not following your last comment – guest271314 Aug 05 '17 at 08:39
  • @Thilo The duplicate vote lacks merit. Again, `.querySelector()` and `.querySelectorAll()` do not appear at text of linked question or answers. Nor is an explanation provided for why `.getElementById()` returns the element while `.querySelector()` and `.querySelectorAll()` throw syntax errors – guest271314 Aug 05 '17 at 08:42
  • `getElementById` is given an id, `querySelector` is given a CSS selector. – Thilo Aug 05 '17 at 08:44
  • 2
    Yea I dont think i typed what I meant. #111 is an id selector, by css spec id selectors are the # followed by a css identifier. So as an id selector it is invalid as identifiers are not allowed to begin with numbers. Now [id="111"] is not an id selector it is an attribute selector which has an additional condition: _"Attribute values must be CSS identifiers or __strings__"_ so that selector is matching a string and not a css identifier – Patrick Evans Aug 05 '17 at 08:45
  • Here is another link for you: https://stackoverflow.com/questions/4132793/special-characters-in-css-selectors – Thilo Aug 05 '17 at 08:46

2 Answers2

2

According to MDN about the HTML id attribute:

This attribute's value must not contain whitespace (spaces, tabs etc.).

That seems to be the only restriction. They have a note that HTML4 was more strict, but that's it.

According to the spec:

The value must not contain any ASCII whitespace.

That means that 111 is a valid value for the HTML id attribute.

querySelector and querySelectorAll however use CSS selectors, which are more strict:

An ID selector consists of a #, followed by a valid identifier.

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".

That means that #111 is not a valid CSS selector. Instead, use:

document.querySelector('[id="111"]')

To answer your question directly:

Is "111" a valid HTML id attribute or are document.querySelector() and document.querySelectorAll() correctly throwing syntax errors?

"111" is a valid attribute, and the syntax error are thrown correctly.

PeterMader
  • 6,987
  • 1
  • 21
  • 31
0

Just ran into the problem that I cannot use GUIDs as ID. If the GUID starts with a letter, it works. If the GUID starts with a digit, it will not work. Maybe one documentation says that a GUID can start with a digit, but not in all case ...

-> The answer in 2020 is: IDs should start with a letter, so that they work reliably everywhere - no matter if CSS, JavaScript - no matter which browser.

Now I simply put a G- in front of my GUIDs.

Sarah Trees
  • 822
  • 12
  • 28