Skip to content

Latest commit

 

History

History
25 lines (18 loc) · 629 Bytes

prefer-query-selector.md

File metadata and controls

25 lines (18 loc) · 629 Bytes

Prefer querySelector over getElementById, querySelectorAll over getElementsByClassName and getElementsByTagName

They are not faster than querySelector and it's better to be consistent.

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');