Search code examples
javascriptmulti-level

how to filter a multilevel menu with a search input


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>


Solution

  • 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...