Find prev () tags with multiple selectors
Let's look at this situation:
<ul> <li>data</li> <li class="selector">data2</li> <li class="selector2">data3</li> </ul> What I'm trying to do is match li , which either has a selector class or has an undefined class attribute, something like this:
jQuery(function($) { $('.selector2').prevAll('li.selector OR li[class==""]'); }); So, if I execute prevAll() on .selector2 , it should return 2 list items. If I ran it on .selector , it should return the first element of the list.
So, is there a way to replace this OR ...?
PS: xpath may work for me too, as I design for modern browsers
jQuery(function($) { $('.selector2').prevAll('li.selector, li:not([class])'); }); Adding an important comment from @pimvdb
This is correct, but be careful - something like
.addClass("foo").removeClass("foo")leaves the class attribute behind, although you (probably) expect it to be in this initial state. So this is not quite the same as[class=''].
What I'm trying to do is match
li, which either has a class of "selector" or have an attribute of class undefined
This XPath expression is equivalent to the pseudo code in the question :
/ul/li[@class='selector2']/preceding-sibling::li[@class='selector' or not(@class)] However, a literal translation of the specified requirement :
/ul/li[@class='selector' or not(@class)]