-2

i have searched here for hours, but none of these jquery scripts explained, to toggle a simple menu with list childs, worked for me. i like to toggle the childs, with help from css.

i have a wordpress menu & the html look like this: (the ul parent element has no class/ or id)

<nav class="nav" role="navigation">
  <div class="menu">
    <ul>
      <li class="page_item page-item-4"><a href="#">Aktuell</a></li>
      <li class="page_item page-item-211 page_item_has_children current_page_item">
        <a href="#">Liquida</a>
        <ul class="children">
          <li class="page_item page-item-222"><a href="#">lager1</a></li>
          <li class="page_item page-item-213"><a href="#">lager2</a></li>
          <li class="page_item page-item-219"><a href="#">lager3</a></li>
        </ul>
      </li>
      <li class="page_item page-item-4444"><a href="#">Last</a></li>
      <li class="page_item page-item-92"><a href="#">Lieferung</a></li>
    </ul>
  </div>
</nav>

to show that the first ul has no class name nor id name, for me this is the problem that i have with this.

RevanProdigalKnight
  • 1,316
  • 1
  • 14
  • 23

1 Answers1

0

Just test if you have a ul.children next to the link you click on, it should do the trick.

In CSS

.children { display: none; }

in jQuery

$('.menu a').click(function(e){
    var childrenList = $(this).next('.children');

    if(childrenList.length) {

        $(childrenList).toggle();
        e.preventDefault();
    }
});

See JSFiddle

Adrian Tombu
  • 696
  • 6
  • 11