I prefer to use document.querySelector() and not just because it's newer. The results are more reliable imo. Single item vs document.querySelectorAll() giving as many as it can find. Also I wouldn't need jQuery or require me to think about it's methods.
I generally agree with using document.querySelector (or document.querySelectorAll, depending on what you need), except in the case where I have an ID or a class in a variable and I have to select by just that. In those cases, I find it cleaner to just use document.getElementById or document.getElementsByClassName as appropriate instead of having to do the extra step of building a selector.
View all comments
Show parent comment
19
u/[deleted] Mar 01 '20
I prefer to use
document.querySelector()and not just because it's newer. The results are more reliable imo. Single item vsdocument.querySelectorAll()giving as many as it can find. Also I wouldn't need jQuery or require me to think about it's methods.$('#element')document.querySelector('#element')