0

here is the code, can someone help to modify?

(function($){
$(document).ready(function() {

// Categories menu opening
// Categories menu opening
$('.woocommerce.widget_product_categories .product-categories li.cat-parent').prepend('<div class="cat-menu-close"></div>');

$( document ).on( "click", ".woocommerce.widget_product_categories .product-categories li.cat-parent:not(.opened) > .cat-menu-close", function(e) {
    $(this).parent().addClass('opened');

    $(this).next().next('ul.children').slideDown();

});
}) (jQuery);

Can someone help me to modify

Adam Lim
  • 51
  • 7
  • So you want this `.cat-menu-close` to both expand **and** collapse the `ul.children`s when clicked? Or where is your code for sliding it up? – Yom T. Jan 24 '19 at 04:17
  • yes, exactly. i have try to put on the sliding up code but it wont work. can u teach me the sliding up code? – Adam Lim Jan 24 '19 at 04:24
  • clicked it will expand while the ul.children is collapse and collapse while it is expand. – Adam Lim Jan 24 '19 at 04:29

1 Answers1

1

To "toggle" element's visibility with sliding effect, you could use slideToggle.

Note that you need to remove :not(.opened) from the selector, because the handler won't work when this parent element has this class.

$(document).on("click", ".woocommerce.widget_product_categories .product-categories li.cat-parent > .cat-menu-close", function(e) {
  var $catParent = $(this).closest('li.cat-parent');
  var state = $catParent.hasClass('close');

  $catParent.toggleClass('opened', !state);

  $(this).nextAll('ul.children:first').slideToggle(state);
});
.cat-menu-close {
  cursor: pointer;
}

ul {
  list-style: none;
}

ul.children {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="woocommerce widget_product_categories">
  <div class="product-categories">
    <ul>
      <li class="cat-parent">
        <span class="cat-menu-close">
          <button>Expand/collapse</button>
        </span>
        
        <div>Random sibling element</div>
        
        <ul class="children">
          <li>Child item</li>
          <li>Child item</li>
          <li>Child item</li>
        </ul>
      </li>
    </ul>
  </div>
</div>
Yom T.
  • 8,760
  • 2
  • 32
  • 49