Skip to content

Latest commit

 

History

History
27 lines (19 loc) · 657 Bytes

prefer-query-selector.md

File metadata and controls

27 lines (19 loc) · 657 Bytes

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

It's better to use the same method to query DOM elements.

This rule is partly fixable.

Fail

document.getElementById('foo');
document.getElementsByClassName('foo bar');
document.getElementsByTagName('main');
document.getElementsByClassName(fn());

Pass

document.querySelector('#foo');
document.querySelector('.bar');
document.querySelector('main #foo .bar');
document.querySelectorAll('.foo .bar');
document.querySelectorAll('li a');
document.querySelector('li').querySelectorAll('a');