useDomQuerySelector (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.4.13 - Diagnostic Category:
lint/style/useDomQuerySelector - This rule isn’t recommended, so you need to enable it.
- This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
- Inspired from
unicorn/prefer-query-selector
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "style": { "useDomQuerySelector": "error" } } }}Description
Section titled “Description”Prefer querySelector() and querySelectorAll() over older DOM query APIs.
This rule prefers querySelector() over getElementById(), and querySelectorAll() over
getElementsByClassName(), getElementsByTagName(), and getElementsByName().
Using the more modern DOM query APIs can often make the intent of a DOM lookup clearer and more concise than the older APIs, especially for complex selectors or if filtering by multiple attributes. Additionally, these newer APIs are more flexible and can be easily refined later with more specific selectors without needing to change the method being called.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”document.getElementById("foo");code-block.js:1:10 lint/style/useDomQuerySelector FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This DOM query uses an older DOM query API.
> 1 │ document.getElementById("foo");
│ ^^^^^^^^^^^^^^
2 │
ℹ Using the more modern DOM query APIs consistently makes DOM lookups easier to read and easier to refine with more specific selectors.
ℹ Unsafe fix: Use .querySelector() instead.
1 │ - document.getElementById("foo");
1 │ + document.querySelector("#foo");
2 2 │
document.getElementsByClassName("foo bar");code-block.js:1:10 lint/style/useDomQuerySelector FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This DOM query uses an older DOM query API.
> 1 │ document.getElementsByClassName("foo bar");
│ ^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Using the more modern DOM query APIs consistently makes DOM lookups easier to read and easier to refine with more specific selectors.
ℹ Unsafe fix: Use .querySelectorAll() instead.
1 │ - document.getElementsByClassName("foo·bar");
1 │ + document.querySelectorAll(".foo.bar");
2 2 │
document.getElementsByTagName("main");code-block.js:1:10 lint/style/useDomQuerySelector FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This DOM query uses an older DOM query API.
> 1 │ document.getElementsByTagName("main");
│ ^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Using the more modern DOM query APIs consistently makes DOM lookups easier to read and easier to refine with more specific selectors.
ℹ Unsafe fix: Use .querySelectorAll() instead.
1 │ - document.getElementsByTagName("main");
1 │ + document.querySelectorAll("main");
2 2 │
document.querySelector("#foo");document.querySelectorAll(".foo.bar");Options
Section titled “Options”ignore
Section titled “ignore”Allow specific variables to use the older DOM query APIs. This is useful if your application has APIs that expose methods with the same names as DOM query APIs.
{ "linter": { "rules": { "style": { "useDomQuerySelector": { "level": "on", "options": { "ignore": [ "store", "customApi" ] } } } } }}These are ignored, so they are not flagged.
store.getElementById("COVER_IMAGE");customApi.getElementsByClassName("item");Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.