DEV Community

Discussion on: querySelectorAll vs getElements

Collapse
 
metruzanca profile image
Samuele Zanca

Either a typo or a misconsception.

There is not "getElementByClassName"

The only "getElement" is getElementById. All others are "getElements*"

On a side note:

Generally you should be using getElement* as they are more performant and the vast majority of cases for selecting elements in modern js is to add event listeners to elements. QuerySelector's main benefit is being able to mix up classes, ids and tag names in their query. e.g. as a stupid example document.querySelectorAll('div div div .row') selects the location and join date of the author of this blog post. However more often than not you'll just be able to getElementsByClassName of the parent element. I'm sure querySelector has some niche use cases e.g. writing code to manipulate markup you didn't write and can't modify for whatever reason. But in general I've always been of the opinion that getElement* > querySelector.
(you can for..of instead of forEach or you can Array.From(...).forEach)

Collapse
 
jasterix profile image
Jasterix

Thanks! This was a typo, but illustrated the point (getElement* has more room for error) beautifully