I have a deeply nested list (10 levels), and I want to filter through that list so that I can get the <li> that I searched for and if that <li> has children I want to show them as well, Here's a code example ...
$(document).ready(function () {
$("#filter").keyup(function () {
var filter = $(this).val();
$("li").each(function () {
if (filter == "") {
$(this).css("visibility", "visible");
$(this).fadeIn();
} else if ($(this).text().search(new RegExp(filter, "i")) < 0) {
$(this).css("visibility", "hidden");
$(this).fadeOut();
} else {
$(this).css("visibility", "visible");
$(this).fadeIn();
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="filter" type="text" />
<ul>
<li>Tom</li>
<li> <a>Peter</a>
<ul>
<li> <a>John</a>
<ul>
<li> <a>Doe</a>
<ul>
<li> <a>Shia</a>
</li>
</ul>
</li>
</ul>
</li>
<li><a>Nicolas</a>
</li>
<li><a>Reem</a>
</li>
</ul>
</li>
<li><a>Danial</a>
<ul>
<li> <a>Adam</a>
</li>
</ul>
</li>
</ul>
In the above example, I managed to get the <li> I searched for, but I can't figure out how to keep it's children visible and open if he has any.
Note: you don't have to re-use the code I posted here, you can post a better, more flexible implementation if you can.