# prefer-query-selector

Rule `unicorn/prefer-query-selector`. Prefer `.querySelector()` and `.querySelectorAll()` over older DOM query methods.

- Source: unicorn
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 12M weekly downloads of presets that enable it
- Trend: +40.1% last quarter
- Rank: #769 by reach, #965 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-query-selector` | no | safe | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-query-selector.md) |
| Oxlint | `unicorn/prefer-query-selector` | no | safe | plugin `unicorn`, category pedantic | [docs](https://oxc.rs/docs/guide/usage/linter/rules/unicorn/prefer-query-selector.html) |
| Biome | `style/useDomQuerySelector` | no | unsafe | group style, since 2.4.13 | [docs](https://biomejs.dev/linter/rules/use-dom-query-selector/) |

## Code examples

### Incorrect: the rule reports this

```js
document.getElementById('foo');
```

```js
document.getElementsByClassName('foo');
```

```js
document.getElementsByClassName('foo bar');
```

### Correct: the rule accepts this

```js
document.querySelector('#foo');
```

```js
document.querySelectorAll('.foo');
```

```js
document.querySelectorAll('.foo.bar');
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/prefer-query-selector": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "rules": {
    "unicorn/prefer-query-selector": "error"
  }
}
```

### Biome: `biome.json`

```json
{
  "linter": {
    "rules": {
      "style": {
        "useDomQuerySelector": "error"
      }
    }
  }
}
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [Ultracite for Biome](https://lintradar.dev/presets/ultracite-biome.md) | Biome | `ultracite` | 616 | 2.1M | +151.7% |
| [Ultracite for Oxlint](https://lintradar.dev/presets/ultracite-oxlint.md) | Oxlint | `ultracite` | 475 | 2.1M | +151.7% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/prefer-negative-index](https://lintradar.dev/rules/unicorn/prefer-negative-index.md): Prefer negative index over `.length - index` when possible.
- [unicorn/explicit-timer-delay](https://lintradar.dev/rules/unicorn/explicit-timer-delay.md): Enforce or disallow explicit `delay` argument for `setTimeout()` and `setInterval()`.
- [unicorn/no-zero-fractions](https://lintradar.dev/rules/unicorn/no-zero-fractions.md): Require consistent decimal numbers without redundant zeros.
- [unicorn/no-abusive-eslint-disable](https://lintradar.dev/rules/unicorn/no-abusive-eslint-disable.md): Enforce specifying rules to disable in `eslint-disable` comments.
- [unicorn/no-unreadable-iife](https://lintradar.dev/rules/unicorn/no-unreadable-iife.md): Disallow unreadable IIFEs.
- [unicorn/no-accessor-recursion](https://lintradar.dev/rules/unicorn/no-accessor-recursion.md): Disallow recursive access to `this` within getters and setters.

---

Web page: https://lintradar.dev/rules/unicorn/prefer-query-selector. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
