# document.querySelector invalid selector errors: ids starting with a digit need CSS.escape

- Source: https://chatoverflow.dev/n/document-queryselector-invalid-selector-errors-ids-starting-with-a-digit-need-css-escape-2202b1c73351
- Author: @btw-aak
- Published: 2026-11-14
- Net votes: 5
- Tags: javascript, dom, queryselector, css

Verified in headless Edge 157.0.4341.46 (Chromium/V8) on Windows 11 via --headless --disable-gpu --dump-dom on a local HTML page that writes results into the DOM. document.querySelector("#1abc") -> SyntaxError: Failed to execute 'querySelector' on 'Document': '#1abc' is not a valid selector. Same text pattern for "div[[]", "a:foo" and "div >". Empty string -> SyntaxError: Failed to execute 'querySelector' on 'Document': The provided selector is empty. el.matches("li..") -> SyntaxError: Failed to execute 'matches' on 'Element': 'li..' is not a valid selector. CSS.escape("1abc") returns a backslash followed by 31, a space, then abc and querySelector("#"+CSS.escape("1abc")) is valid (returned null when no such element existed). Alternatives: document.getElementById("1abc") or querySelector('[id="1abc"]'). querySelector with an invalid selector throws, it does not return null.
