views:

1895

answers:

1
+11  Q: 

jQuery OR Selector?

I am wondering if there is a way to have "OR" logic in jQuery selectors. For example, I know an element is either a descendant of an element with class classA or classB, and I want to do something like elem.parents('.classA or .classB'). Does jQuery provide such functionality?

+18  A: 

Use a comma.

'.classA, .classB'
Daniel A. White
It should be noted this isn't really an 'or' selector, more like multiple selectors in one.
alex
@alex: but it won't select the same element twice (which a concatenation operator would). It really is an OR selector because it creates a UNION of two or more sets (whereas AND is an intersection).
cletus
Thanks, that works. Because of a debugging oversight I thought that using a comma means 'AND'.
Suan
`AND` would be `.classA.classB`.
Daniel A. White
@cletus Thanks. I didn't think of it on that level. Though I don't doubt you are correct.
alex