In the below code How do i go about selecting the "title" class but IF the "subnav" class exists?
<ul class="flexMenu-popup">
<li>
<a href="x" class="title">
</a>
<ul class="subnav">
</ul>
</li>
</ul>
In the below code How do i go about selecting the "title" class but IF the "subnav" class exists?
<ul class="flexMenu-popup">
<li>
<a href="x" class="title">
</a>
<ul class="subnav">
</ul>
</li>
</ul>
Using adjacent selectors (~ and +) it is only possible to select siblings that follow an element. That means you can select a
that follows a ul
and not vice-versa.
See snippet below:
ul {
border: 1px solid black;
list-style-type: none;
}
.flexMenu-popup li > ul ~ a {
color: red;
}
<ul class="flexMenu-popup">
<li>
<a href="#">a tag</a>
<ul>
ul tag
<li>
<a href="#">a tag</a>
<ul>ul tag</ul>
<a href="#">a tag</a>
</li>
</ul>
<a href="#">a tag</a>
</li>
</ul>
Solution: But I think what you are looking for can be done by the combination of the below rules:
.flexMenu-popup li > a {
color: red;
}
.flexMenu-popup li > ul ~ a {
color: inherit;
}
See snippet below:
ul {
border: 1px solid black;
list-style-type: none;
}
.flexMenu-popup li > a {
color: red;
}
.flexMenu-popup li > ul ~ a {
color: inherit;
}
<ul class="flexMenu-popup">
<li>
<a href="#">a tag</a>
<ul>
ul tag
<li>
<a href="#">a tag</a>
<ul>ul tag</ul>
<a href="#">a tag</a>
</li>
</ul>
<a href="#">a tag</a>
</li>
</ul>
I'm not sure this is possible with CSS, but it's quite simple with jQuery:
var $ul = $('a.title', $('ul.flexMenu-popup > li > ul.subnav').parent());
if ($ul.length) {
alert('yes');
}
else {
alert('no');
}