> 2. For the method Sanford provided - parentWithClassById(id) - I'm
> concerned about some potential speed issues on a page with
> potentially thousands of elements (and potentially in a loop).
> Maybe some clarification with some methods would help.
Potential speed issues? That function will use the browser's native
querySelectorAll when available. You are never going to get faster
results from an all- or part-userland tree walk. Practically
equivalent, maybe, but not faster (barring DOM caching in userland,
which invalidates the comparison).
I already mentioned that the performance of parentWithClassById
significantly outstrips the more script-heavy suggestions originally
thrown out (running 500 iterations to draw out clear differences). I
didn't time it against your functions, but it's easy for you to test.
> $$('.theClass') ?= document.getElements('.theClass') ?=
> document.getElements('[class=theClass]')
> Aren't all of these equivalent?
No, the 3rd one should be [class~=theClass].
There's really no utility to using an attribute selector on class
instead of a class selector unless you want to confuse yourself -- and
hurt performance in old/dumb browsers that both don't have qSA() and
can't smarten up the query and run it through gEBCN().
> Also, parentWithClassById() won't work if the element doesn't have an id.
Ah, c'mon... are you not responsible for authoring/recommending the
markup? :P
Even if you're not, if you have a handle to the target element, add an
ID to it if need be... though I suppose that would counteract the
speed advantage.
-- S.