Reputation: 87
I have 2 hidden DIVs
every DIV contains a SPAN
I want to show only the DIV who has the SPAN with class RedBackground
The javascript code i wrote is not working
$("div span.RedBackground").show();
.greenBackground {
color: green;
}
.RedBackground {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="display: none;">
<span class="RedBackground">show this</span>
</div>
<div style="display: none;">
<span class="GreenBackground">NOT show this</span>
</div>
Upvotes: 1
Views: 29
Reputation: 12452
Use :has
selector to get all div
containing such element. Not need to use things like parent
or closest
:
$("div:has(span.RedBackground)").show();
.greenBackground { color: green; }
.RedBackground { color: red; }
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="display: none;">
<span class="RedBackground">show this</span>
</div>
<div style="display: none;">
<span class="GreenBackground">NOT show this</span>
</div>
Upvotes: 2
Reputation: 15565
$("span.RedBackground").parent('div')
to show div. means span.RedBackground
's parent div which is hidden is shown$("div span.RedBackground").show();
means div with span.RedBackground
but the div is hidden so you cant still see any closest("div")
$("span.RedBackground").parent('div').show();
.greenBackground {
color: green;
}
.RedBackground {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="display: none;">
<span class="RedBackground">show this</span>
</div>
<div style="display: none;">
<span class="GreenBackground">NOT show this</span>
</div>
Upvotes: 0