With jQuery I want to hide all li elements and show only li with class .selected and others elements that have a minus (-) symbol.
This is the result I expect when I click on the button:
<ul>
<li class="selected">Item 5</li>
<li>- Item 6</li>
<li>- Item 7</li>
<li>- Item 8</li>
</ul>
<input type="button" id="btm" value="show" />
var $j = jQuery.noConflict();
$j(document).ready(function() {
$j("input#btm").bind("click", function() {
for (var i = 1; i <= 12; i++) {
if ($j('.chzn-results li:nth-child(' + i + ')').hasClass("selected") || $j('.chzn-results li:nth-child(' + i + ')').text().indexOf('- ') >= 0) {
$j('.chzn-results li:nth-child(' + i + ')').show();
}
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="chzn-results">
<li>Item 1</li>
<li>- Item 2</li>
<li>- Item 3</li>
<li>- Item 4</li>
<li class="selected">Item 5</li>
<li>- Item 6</li>
<li>- Item 7</li>
<li>- Item 8</li>
<li>Item 9</li>
<li>Item 10</li>
<li>Item 11</li>
<li>- Item 12</li>
</ul>
<input type="button" id="btm" value="show" />