In a piece of example code I wrote
var as = toArray(document.getElementsByClassName("false")).filter(function (el) {
return el.tagName === "A";
});
And I was thinking I could replace that with
var as = document.querySelectorAll("a.false");
Now after reading the following facts
- Pretend browser support isn't an issue (we have shims and polyfills).
- Pretend your not in your generic jQuery mindset of you shall use QSA for getting every element.
- I'm going to write
qsa
instead ofdocument.querySelectorAll
because I'm lazy.
Question: When should I favour QSA over the normal methods?
It's clear that if your doing qsa("a")
or qsa(".class")
or qsa("#id")
your doing it wrong because there are methods (byTagName, byClassName, byId) that are better.
It's also clear that qsa("div > p.magic")
is a sensible use-case.
Question: But is qsa("tagName.class")
a good use-case of QSA?
As a futher aside there are also these things called NodeIterator
I've asked a question about QSA vs NodeIterator
getElementsByTagNameAndClassName()
becomes available,querySelectorAll("a.false")
makes sense and is more readable than a chained call fromtoArray()
tofilter()
. – Cepheus.filter
is also slow as hell. – ShantagetElementsByTagNameAndClassName()
manifests itself, you don't have much choice outside ofquerySelectorAll()
, do you? – Cepheus