Let's face 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
s that either have selector
class or have class attribute undefined
, something like this:
jQuery(function($) {
$('.selector2').prevAll('li.selector OR li[class==""]');
});
So if I'm running prevAll()
on the .selector2
, it should return 2 list items. If i run it on .selector
, it should return the first list item.
So is there a way to replace that OR ... ?
PS: xpath may work for me too as i'm developing for modern browsers
jQuery(function($) {
$('.selector2').prevAll('li.selector, li:not([class])');
});
DEMO
This is correct, but be careful - something like
.addClass("foo").removeClass("foo")
leaves the class attribute behind, although you (might) expect it to be in it's initial state. So it's not quite the same as[class='']
.
What i'm trying to do is match
li
s that either have "selector" class or have class attribute 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 quoted requirement is:
/ul/li[@class='selector' or not(@class)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With