Skip to content

Latest commit

Β 

History

History
49 lines (35 loc) Β· 1.3 KB

File metadata and controls

49 lines (35 loc) Β· 1.3 KB

Prefer .querySelector() over .getElementById(), .querySelectorAll() over .getElementsByClassName() and .getElementsByTagName() and .getElementsByName()

πŸ’ΌπŸš« This rule is enabled in the βœ… recommended config. This rule is disabled in the β˜‘οΈ unopinionated config.

πŸ”§ This rule is automatically fixable by the --fix CLI option.

It's better to use the same method to query DOM elements. This helps keep consistency and it lends itself to future improvements (e.g. more specific selectors).

Examples

// ❌
document.getElementById('foo');

// βœ…
document.querySelector('#foo');
// ❌
document.getElementsByClassName('foo');

// βœ…
document.querySelectorAll('.foo');
// ❌
document.getElementsByClassName('foo bar');

// βœ…
document.querySelectorAll('.foo.bar');
// ❌
document.getElementsByTagName('main');

// βœ…
document.querySelectorAll('main');
// ❌
document.getElementsByClassName(fn());