I’m trying to make collapsing sidebar of product categories. by using woocommerce product categories widget and a bit of code (without coding the sidebar showing all the categories & sub-categories one above another). I achieved to make a collapsing sidebar but when I click one main category – all the categories opened at once and all subcategories appear.
I didn’t find a way to give each sub-category group a unique class using jQuery only – all by woocommerce default has calls called "children".
(function($) {
$(document).ready(function() {
$("li.cat-item-22").click(function() {
$('ul.children').slideToggle();
});
$("li.cat-item-29").click(function() {
$('ul.children').slideToggle();
});
$("li.cat-item-30").click(function() {
$('ul.children').slideToggle();
});
$("li.cat-item-40").click(function() {
$('ul.children').slideToggle();
});
$("li.cat-item-43").click(function() {
$('ul.children').slideToggle();
});
$("li.cat-item-55").click(function() {
$('ul.children').slideToggle();
});
$("li.cat-item-61").click(function() {
$('ul.children').slideToggle();
});
});
})(jQuery);
2
Answers
It finally worked with the function below - that way i could give each 'children' class a unique id that solved my problem.
You have to use attribute selector to get your object
https://api.jquery.com/attribute-starts-with-selector/
And then use $(this) to refer to you current object with click event