-1

The difficulty is that the menu has many levels(like the menu in matrix-admin),and every level is large; how should i filter filter the menu on the basis of the value of the search input.

and the output should be like this:(http://www.jqueryrain.com/?lEMFOjZ1) and my html is like this:

<ul>
<li>xxx</li>
<li>
   <a>xxx</a>
   <ul>
      <li>
           <a>xxx</a>
           <ul>
                <li>
                   <a>xxx</a>
                   <ul></ul>
                </li>
           </ul>
      </li>
      <li>xxxx</li>
   </ul>
</li>

user3796331
  • 3
  • 1
  • 3

2 Answers2

5

Demo Fiddle

$("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();
    }
});  

Onkeypress event the menu will be filtered.... The parent li will be visible, if you try find the child li...

Community
  • 1
  • 1
Bhavik
  • 4,836
  • 3
  • 30
  • 45
1

You don't need jquery for this.

document.querySelector("#filter").addEventListener("keyup", function(e) {
  var filter = this.value;
  var count = 0;
  document.querySelectorAll("li").forEach(function(li) {
    if (filter == "") {
      li.style["display"] = "list-item";
    } else if (!li.textContent.match(filter)) {
      li.style["display"] = "none";
    } else {
      li.style["display"] = "list-item";
    }
  });
});

https://jsfiddle.net/gfe9y5o2/

lbenedix
  • 11
  • 1
  • How to use it on particular div or ul using id? its searching whole li on the page. – Pankaj Jul 18 '18 at 16:49
  • just add a class to your `
      ` and add this to the `document.querySelectorAll` Here is an example: [https://jsfiddle.net/gfe9y5o2/42/](https://jsfiddle.net/gfe9y5o2/42/)
    – lbenedix Jul 20 '18 at 11:32