The main differences are basically two:
Example:
// buscar por elementos com a classe "abc"
let abcLiveList = document.getElementsByClassName('abc');
console.log('getElementsByClassName: ');
for (const e of abcLiveList) {
console.log(`- ${e.innerText}`);
}
let abcFixedList = document.querySelectorAll('.abc');
console.log('querySelectorAll: ');
for (const e of abcFixedList) {
console.log(`- ${e.innerText}`);
}
// atualizando o DOM (adicionar novo elemento)
let span = document.createElement('span');
span.classList.add('abc');
span.innerHTML = 'novo';
document.body.appendChild(span);
console.log('getElementsByClassName (atualizado com o novo elemento): ');
for (const e of abcLiveList) {
console.log(`- ${e.innerText}`);
}
console.log('querySelectorAll (não é atualizado com o novo elemento): ');
for (const e of abcFixedList) {
console.log(`- ${e.innerText}`);
}
<div class="abc">olá</div>
<p class="abc">tudo bem?</p>
<p>blá</p>
Note that in getElementsByClassName('abc')
I passed the class name 'abc'
, already in querySelectorAll('.abc')
the class name needed a point before ('.abc'
), for this is the selector syntax to search for a class name.
See also that the list returned by getElementsByClassName
changes as DOM changes: when adding a new element with the "abc" class, it becomes part of the list automatically. Already the list returned by querySelectorAll
does not change, regardless of the changes made in the DOM (if I wanted to have the new element, I would need to do a new search).
Which one should I use?
It depends on what you need. Do you want the list to be updated as the DOM changes? Want to search only by class name or use some more complex criteria? (for example, only tags div
with the class "abc" - in this case, querySelectorAll
would be the most appropriate) - and so on. There is no right answer, it depends on the need.
As for being more performative, there are tests that say querySelectorAll
is slower, but that shouldn’t be the only criterion. If it is a "very large" page with multiple searches being made, there may be some noticeable delay, but anyway, performance is something that should be tested case by case, you should not choose to use one or the other before testing (first choose what makes the most sense, and if you detect that this is the cause of bad performance, then you change).
The difference is that
querySelectorAll
can be used for selectors other than class only. And even being class can specify hierarchy for example using space or>
– Isac